INTERFACING PYTHON TO MYSQL
CLASS-XII
BY- REETA (PGT IT)
JNV EAST GODAVARI-2
ANDHARA PRADESH
Before we start let us know bit about
MYSQL
MySQL is the most popular open Source Relational SQL
Database Management System.
Features:
It is written in C and C++.
MySQL is quicker than other databases so it can work
well even with the large data set.
MySQL supports many operating systems with many
languages like PHP,PERL,C,C++,JAVA etc.
MySQL uses a standard form of well-known SQL data
language.
MySQL is an open-source database, so it is available
free of cost.
Interface python with SQL DB
All companies
whether large or small
use data bases. So it
become necessary to
develop
project/software
using any
programming
language like python in such a manner which can interface with
such databases which support SQL
Steps to connect python to
MySQL DB
1. Install MySQL connector drivers.
2. Import Mysql . connector module
3. Create a connection object.
4. Create a cursor instance.
5. Execute a query.
6. Extract data from the resultset.
7. Clean up the environment.
Connecting to MySQL From Ptyhon
MySQL python connector :- To access the MySQL database
from python, you need a database driver. Mysql connector is
a standardize database driver provided by MySQL.
Step 1.Open command window and Install MySQL Drivers
through pip command.
Pip install mysql-connector
Check whether MySQL-Connector
is installed properly
Step 2. Import drivers in python IDLE
i. Open Python IDLE
ii. Type import mysql . connector
If no error occures,it means mysql-connector is
working properly
Creating the connection : connect()
Step-3. Pass the database details like hostname, username
and database password in the method call. the method
returns the connection object:
mydb=mysql . connector. connect(host="localhost",
user="root", passwd = "root")
print(mydb)
Output given below shows no error in connection
Or use following command to check connection is
successful or not
Creating Cursor
Step 4. To have multiple separate working environment
through the same connection to the database.
Database cursor can be
created to traverse the resultset(set of records retrieved as
per query) row by row. Cursor instance can be created by
using cursor() as follow:
Syntax - <cur_obj>=conn.cursor()
e.g. - mycursor= mydb. Cursor()
Note: We need a cursor to execute our MySQL queries
Execute sql quary
Step 5 . Execute sql quary
Mycursor= mydb. cursor()
Mycursor.execute(‘CREATE DATABASE school’)
# Data base Created
Extract data from Resultset
Step 6.Extract data from Resultset:
# list the all databases created
Creating Table
Extract data from resultset
Fetchall():- Displaying all records.
Fetch all (remaining) rows of a query result, returning
them as a sequence(e.g. a list of tuples).
Output:
Cursor.rowcount()
• It will display no of records fetched after running.
More Functions
cursor . fetchall(): fetches all the rows of a query result. it
returns all the rows as a list of tuples.An empty list is
returned if there is no record to fetch.
cursor . fetchmany(size): returns the number of rows
specified by the size argument. when called repeatedly
this method fetches the next set of rows of a query result
and returns a list of tuples.if no more rows are available, it
reruns an empty list.
cursor . fetchone(): returns a single record or none if no
more rows are available.
Clean up the Environment
After doing all the processing in this final step, you need
to close the connection established. BY using following
command:-
mycursor. Close()
Parameterised Queries
String templates with % formatting
Parameterised Queries
String formatting with .format()
Performing insert and update queries
Always commit() after executing insert and update
queries. Syntax:-
connectionboject.commit()
1.INSERT:
Performing insert and update queries
2.UPDATE