Chapter 10 - Interface Python With MySQL
Chapter 10 - Interface Python With MySQL
In order to connect to a database from within Python, you need a library(mysql connector) that
provides connectivity functionality.
There are mainly seven steps that must be followed in order to create a database connectivity
application.
- Start Python’s editor where you can create your Python scripts.
Step 2. Import mysql.connector Package
- First of all you need to import mysql.connector package in your Python scripts. For this, write
import command as shown below:
import mysql.connector
or
import mysql.connector as sqLtor
The above command will establish connection to MySQL database with user as “root” , password
as “MyPass” and to the MySQL database namely test which exists on the MySQL.
P a g e 1|7
- You can also check for successful connection using function is_connected( ) with connected
object, which returns True , if connection is successful.
e.g.
- E.g.
The above code will execute the given SQL query and store the retrieved records(i.e. , the resultset)
in the cursor object (namely cursor) which you can then use in your program/scripts as required.
P a g e 2|7
(iii) <data>=<cursor>.fetchmany(<n>) - This method accepts number of records to fetch and
returns a tuple where each record itself is a tuple.
(iv) <variable>=<cursor>.rowcount – The rowcount is a property of cursor object that returns
the number of rows retrieved from the cursor so far.
For Example,
P a g e 3|7
(ii) The fetchmany( ) method
P a g e 4|7
Step 7: Clean up the Environment
- In this final step, you need to close the connection established. This you can do as follows:
<connection object>.close( )
- E.g.
mycon.close( )
Parameterised Queries
E.g.
e.g.
Now you can store this query string in variable and then execute that variable through
cursor.execute( ) method as shown below :
P a g e 5|7
- In the similar manner, you can add multiple parameter values, but you must not forget to enclose
placeholder %s in quotes for string parameters e.g.
e.g.1.
e.g.2.
Output:
e.g.3.
P a g e 6|7
Performing INSERT and UPDATE Queries
- Insert and Update SQL commands, can also executed using SELECT queries.
- But after executing INSERT and UPDATE queries you must commit your query. This makes the
changes made by the INSERT and UPDATE queries permanent. For this you must run commit( )
method , i.e.
<connection object>.commit( )
Important Questions
Q1. What is database connectivity ?
Ans. Database connectivity refers to connection and communication between an application and a
database system.
Q2. What is Connection ? What is its role?
Ans. A Connection (represented through a connection object) is the session between the application
program and a database. To do anything with database, one must have a connection object.
Q3. What is a result set?
Ans. A result set refers to a logical set of records that are fetched from the database by executing a
query and made available to the application-program.
Q4. Which package must be imported in Python to create a database connectivity application?
Ans. One such package is mysql.connector
*************************
P a g e 7|7