INTERFACE PYTHON WITH
MYSQL CONNECTOR
Python mySql connectivity
• Connect mysql connector to python idle with the following command:
pip install mysql-connector
Note: to check type in idle import [Link]
STEPS FOR CREATING DATABASE CONNECTIVITY
IMPORT THE PACKAGE [Link]
OPEN A CONNECTION TO A DATABASE
CREATE A CURSOR INSTANCE
EXECUTE A QUERY
EXTRACT DATA FROM RESULT SET
CLEAN UP THE ENVIRONMENT
Step 1: Start Python
Step 2: Import the packages required for database program
Step 3: Open a connection to database
Step 4: Create a cursor instance
Step 5: Execute a Query
Step 6: Extract data from result set
Step 7: Clean up the environment
CONNECTION TO MYSQL DATABASE
con=[Link](host=<hostname> ,
user = <username>,
pwd = <password>,
database = <database name>
con=[Link](host=‘localhost’, user = ‘root’ ,
pwd=‘root’, database=“test”)
IMPORT MYSQL CONNECTOR
import [Link]
(or)
import [Link] as sqltor
con=[Link] (host=‘local host’, user=“root”,pwd=“root”)
if con.is_connected():
print(“success”)
CURSOR INSTANCE
• A database cursor is a special control structure that facilitates the row by
row processing of the records,
• Syntax: cursorobject=[Link]()
import [Link]
con=[Link](host="localhost",user="root",pa
ssword="root",database="abc")
cur=[Link]()
[Link]("select * from emp")
data=[Link]()
print(data)
• [Link]() : Returns all the records from the file in a tuple form
• [Link](): It will return one record from the result set as a tuple
Note: first time it will fetch first record, next time it will fetch second record
• [Link] (): This method will fetch ‘n’ number of records
• [Link]: It returns [Link] records retrieved from the table
fetchall(),rowcount
import [Link]
con=[Link](host="localhost",user="root",password="root",databas
e="abc")
cur=[Link]()
[Link]("select * from consignee")
data=[Link]()
for i in data:
print(i)
print("total [Link] rows in the table",[Link])
fetchmany(),
import [Link] as a
con=[Link](host="localhost",user="root",password="root",database="abc")
print("database connected")
cur=[Link]()
[Link]("select * from faculty")
#data=[Link](2)
for i in data:
print(i)
fetchone()
import [Link] as a
con=[Link](host="localhost",user="root",password="root",database="abc")
print("database connected")
cur=[Link]()
[Link]("select * from faculty")
data=[Link]()
print(data)
data=[Link]()
print(data)
Note: After all the process of your program, in this final step you need to close the application
with [Link]()
Parameterized queries
Forming query strings:
i. Old style: string templates with % formatting
Ex: “select * from student where marks > %s” %(70,)
i. New style: String templates with % formatting
Ex: “select * from student where marks > { } and section=’{}”.format(70,’A’)
CREATING DATABASE TABLE AND INSERTING VALUES
import [Link] as a
con=[Link](host="localhost",user="root",password="root",database="abc")
print("database connected and created successfully")
cur=[Link]()
[Link]("create table faculty(fno int, fname varchar(30), fsal float)")
print("database table faculty created")
fno=int(input("enter faculty number"))
fname=input("enter faculty name")
fsal=input("enter faculty salary")
query="insert into faculty values({},'{}',{})".format(fno,fname,fsal))
[Link](query)
print("row inserted successfully")
[Link]()
CREATING DATABASE TABLE AND INSERTING VALUES
import [Link] as a
con=[Link](host="localhost",user="root",password="root",database="abc")
print("database connected and created successfully")
cur=[Link]()
#[Link]("create table faculty(fno int, fname varchar(30), fsal float)")
print("database table faculty created")
while True:
fno=int(input("enter faculty number"))
fname=input("enter faculty name")
fsal=input("enter faculty salary")
query="insert into faculty values({},'{}',{})".format(fno,fname,fsal)
[Link](query)
print("row inserted successfully")
[Link]()
ch=input("do you want to continue, press Y or N")
if ch=='n' or ch=='N':
break