You are on page 1of 10

 What is database connectivity

 It is a connection and communication between an application and a


database system.
 To connect from python to mysql, we need a library called mysql
connector.
 How to install mysql connector:
 Go to following path: C:\xampp\mysql\scripts and type cmd
 On cmd type: pip install mysql-connector-python and press enter
 On python idle write: import mysql.connector and press enter
Steps to create database connectivity

1. Start python
2. Import the package
3. Open a connection
4. Create a cursor instance
5. Execute a query
6. Extract data from result set
7. Clean up the environment
 What is connection object?
 It controls the connection to the database. It is the
session between the application program and the
database. To do anything with database, one must have
a connection object.
 What is cursor?
 Special control structure that facilitates the row by row
processing of records in the resultset that is the set of records
retrieved as per query.
 What is a result set?
 It is a logical set of records that are fetched from the database
by executing a query and made available to the application
program.
1. Import package: import mysql.connector
2. Open a connection:
connection_object=mysql.connector.connect(host="localhost",user="roo
t",passwd="",database="test")
3. Create a cursor: cursor_object=connobject.cursor()
4. Execute query: cursor_object. execute("select * from stu")
5. Clean up the environment: connection_obj.close()
Extract data:
 fetchall(): return all the records from the resultset in a tuple form.
Data=cursor_obj.fetchall()
 fetchone(): return one record from the resultset as a tuple or list.
First time it will return first record, next time it will return next
record and so on. If there are no more records then it return none.
 fetchmany(n): it return the n number of records from the resultset
in the form of tuple. If there are no more records then it return an
empty tuple.
 rowcount: is a property of a cursor object that return the number of
rows retrieved from the cursor so far. Cursor_obj.rowcount
import mysql.connector as m

mycon=m.connect(host="localhost",user="root",passwd="",database="test")

if mycon.is_connected()==False:

print("error connecting to sql")

else:

print("successfully connected to mysql")

cursor=mycon.cursor()

cursor.execute("select * from stu")

data=cursor.fetchone()

c=cursor.rowcount

print("total row is:",c)

print(data)
data=cursor.fetchmany(2)
c=cursor.rowcount
print("total row is:",c)
for row in data:
print(row)
data=cursor.fetchall()
c=cursor.rowcount
print("total row is:",c)
for row in data:
print(row)
mycon.close()
Parameterised query
 Method 1: string template with % formatting f%v
• cursor.execute("select * from stu where id>%s" %i)
• cursor.execute("select * from stu where id>%s and name=‘%s’ "%(i,’j’)) or
 Method 2
•i=(2,) i’s value in tuple
cursor.execute("select * from stu where id>%s”,i)
• i=(2,"ghg")
cursor.execute("select * from stu where id>%s and name=%s",i)
 Method 3(new)
• cursor.execute("select * from stu where id>{} or name='{}'".format(1,'ghjh'))
•Syntax: template.format(p0,p1,...,k0=v0,k1=v1)
• {}:place holder
• Template: is a string containing a mixture of one or more format codes
embadded in constant text.
• Format(): uses its arument to substitue an appropriate value for each format
code in the template.
• cursor.execute("select * from stu where id>{} or name='{}' “
.format(id=1,name='ghjh'))
Insert and update query
 For above queries, we must run commit() with the
connection object.
 Insert:
St=“insert into student values({},’{}’)”.format(1,’fg’)
cursor.execute(st)
mycon.commit()
 Update:
St=“update stu set id={} where name=‘{}’”.format(4,’gh’)
cursor.execute(st)
mycon.commit()

You might also like