Interfacing Python With MySql
Interfacing Python With MySql
A. There are multiple packages available through which database connectivity applications can be
created in Python. One such package is mysql.connector.
Step 6.Extract data from result set (in case of SELECT query)
A. The connect () method of the mysql.connector module is used to connect or establish a connection
with MySQL database.
A. A Connection (represented through a connection object) is the session between the application
program and the database. To do anything with database, one must have a connection object.
A database cursor is a special control structure that facilitates the row by row processing of records in
the resultset.
A. 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.
The role of execute () is execution of queries which are MySQL queries in this chapter along with Python
interface.
Once the result of a query is available in the form of a resultset stored in a cursor object, we can extract
data from the resultset using any of the following methods:
a. fetchall() :- The fetchall() method return all the rows from the result set in the form of a list of tuples,
where each tuple contains a record.
b. fetchone() :- The fetchone() method will return only one row from the result set in the form of tuple
containing a record.
c. fetchmany(n):- The fetchmany() method returns n number of rows from the result set in the form of a
list of tuples, where each tuple contains a record.
When no argument is provided it returns one record, as a list with a single tuple.
rowcount property always returns the number of records that have been retrieved so for using any of
the fetch..() methods.
import mysql.connector
db = mysql.connector.connect(….)
cursor = db.cursor()
db.commit()
db.close()
A. This above program insert a new records in table staff such that person_id and lastname are input by
user.