Python Connectivity
Python Connectivity
Python Connectivity
Now in the next section of Python MySQL connectivity class 12 you will
learn how to establish a connection between Python and MySQL.
Establish a connection
After doing this, check for the errors if any. If your program runs without
errors that means connection is established. Although you can use
is_connected() function to check whether the connection is established or
not! Observe this code:
The next step after the successful connection is to write SQL command
and fetch rows. The SQL commands are used to perform DML operations
and fetch rows read data from table. So we will see them in detail later.
You have to create a cursor object for executing SQL command and fetch
rows. Cursor object is a special kind of structure that processes the data
row by row in database. You can create cursor object in the following
manner.
cur=cn.cursor()
In the next section of Python MySQL connectivity class 12 you will learn
how to perform DML opeations.
To perform the DML operations like insert, update or delete follow these
steps:
1. Create a cursor object
2. Write command as parameters for execute() function
3. Use commit() function to save the changes and reflect the data in
the table.
Let’s DML operations with insert command for Python MySQL connectivity
class 12.
insert command
update command
delete command
Select Command
As you know the select command is used retrieve records from the
database. The result is available in the resultset or dataset. You can store
the select the command in cursor object in python. Then for resultset you
can use the fetch…() function. These are:
cn=msql.connect(host='localhost',user='root',passwd='MySQL@123',da
tabase='Tutorialaicsip')
cur=cn.cursor()
d=cursor.fetchall()
for r in d:
print(r)
cn=msql.connect(host='localhost',user='root',passwd='MySQL@123',da
tabase='Tutorialaicsip')
cur=cn.cursor()
d=cursor.fetchmany(3)
for r in d:
print(r)
import time
cn=msql.connect(host='localhost',user='root',passwd='MySQL@123',da
tabase='Tutorialaicsip')
cur=cn.cursor()
d=cur.fetchone()
print(d)
time.sleep(3)
d=cur.fetchone()
print(d)
time.sleep(3)
d=cur.fetchone()
time.sleep(3)
print(d)
Parametrized Queries
Sometimes we need to access values as per the user’s input. The query
result is based on the values user has passed. So for that we have this
option parameterized queries. There are two ways to use parameterized
queries:
This pattern takes the general form – f % v, where f is a format and v is the
value. Consider the following code:
import time
cn=msql.connect(host='localhost',user='root',passwd='MySQL@123',da
tabase='Tutorialaicsip')
cur=cn.cursor()
d=cur.fetchall()
for r in d:
print(r)
d=cur.fetchall()
for r in d:
print(r)
In this pattern you can write {} where the value is placed followed by
.format(values). Consider the following code:
import time
cn=msql.connect(host='localhost',user='root',passwd='MySQL@123',da
tabase='Tutorialaicsip')
cur=cn.cursor()
d=cur.fetchall()
for r in d:
print(r)
d=cur.fetchall()
for r in d:
print(r)
The last step for Python MySQL connectivity class 12 is closing the
connection.
Finally, you have to close the established connect using close() function. It
will help to clean up the memory. Observe the following code:
con.close()