Aim : EXTRACT THE DATA FROM DATABASE USING PYTHON
===============================
Source Code:
===============================
First you need to write SQL code in the mysql
Create a account give name as localhost
Give a password which will be used for connecting to database.
The SQL code is:-
Create database student1;
Show databases;
Use student1;
Create table student1(Stu varchar(20),sname varchar(40));
Insert into student1 values('s01','himanshu yadav');
Insert into student1 values('s02','virat');
Insert into student1 values('s03','msd');
Insert into student1 values('s04','sachin');
Insert into student1 values('s05','ram');
Insert into student1 values('s06','surya Kumar yadav');
===============================
Source Code :
===============================
import mysql.connector as connector
# Create the connection object
myconn=connector.connect(host = "localhost", user = "root",password = "pass",database="student1")
# Creating the cursor object
cur = myconn.cursor()
# Executing the query
cur.execute("select * from student1")
# Fetching the rows from the cursor object
result = cur.fetchall()
print("Student Details are :")
# Printing the result
for x in result:
print(x);
# Commit the transaction
myconn.commit()
# Close the connection
myconn.close()
Output:
IN THE ABOVE CODE YOU WILL CONNECT THE DATABASE USING PYTHON.
YOU USE MYCONN KEYWORD TO CONNECT THE PROGRAM BY DATABASE.