Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Python Interface - with mysql connector

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Interface - with mysql connector

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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 mysql.connector


STEPS FOR CREATING DATABASE CONNECTIVITY

IMPORT THE PACKAGE MYSQL.CONNECTOR

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=mysql.connector.connect(host=<hostname> ,
user = <username>,
pwd = <password>,
database = <database name>

con=mysql.connector.connect(host=‘localhost’, user = ‘root’ ,


pwd=‘root’, database=“test”)
IMPORT MYSQL CONNECTOR
import mysql.connector
(or)
import mysql.connector as sqltor
con=mysql.connector (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=cursorobject.connect()
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",pa
ssword="root",database="abc")
cur=con.cursor()
cur.execute("select * from emp")
data=cur.fetchall()
print(data)
• cursor.fetchall() : Returns all the records from the file in a tuple form

• cursor.fetchone(): 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

• cursor.fetchmany (): This method will fetch ‘n’ number of records

• cursor.rowcount: It returns no.of records retrieved from the table


fetchall(),rowcount
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password="root",databas
e="abc")
cur=con.cursor()
cur.execute("select * from consignee")
data=cur.fetchall()
for i in data:
print(i)
print("total no.of rows in the table",cur.rowcount)
fetchmany(),
import mysql.connector as a
con=a.connect(host="localhost",user="root",password="root",database="abc")
print("database connected")
cur=con.cursor()
cur.execute("select * from faculty")
#data=cur.fetchmany(2)
for i in data:
print(i)
fetchone()
import mysql.connector as a
con=a.connect(host="localhost",user="root",password="root",database="abc")
print("database connected")
cur=con.cursor()
cur.execute("select * from faculty")
data=cur.fetchone()
print(data)
data=cur.fetchone()
print(data)

Note: After all the process of your program, in this final step you need to close the application
with con.close()
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 mysql.connector as a
con=a.connect(host="localhost",user="root",password="root",database="abc")
print("database connected and created successfully")
cur=con.cursor()
cur.execute("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))
cur.execute(query)
print("row inserted successfully")
con.commit()
CREATING DATABASE TABLE AND INSERTING VALUES

import mysql.connector as a
con=a.connect(host="localhost",user="root",password="root",database="abc")
print("database connected and created successfully")
cur=con.cursor()
#cur.execute("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)
cur.execute(query)
print("row inserted successfully")
con.commit()
ch=input("do you want to continue, press Y or N")
if ch=='n' or ch=='N':
break

You might also like