0% found this document useful (0 votes)
4 views

Interface Python With SQL

The document outlines the steps to interface Python with a MySQL database, including installation of MySQL connector drivers, creating a connection, executing SQL queries, and extracting data. It details the process of creating a cursor for database operations and provides methods for fetching results. Additionally, it emphasizes the importance of closing connections and committing changes after insert or update operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Interface Python With SQL

The document outlines the steps to interface Python with a MySQL database, including installation of MySQL connector drivers, creating a connection, executing SQL queries, and extracting data. It details the process of creating a cursor for database operations and provides methods for fetching results. Additionally, it emphasizes the importance of closing connections and committing changes after insert or update operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INTERFACING PYTHON TO MYSQL

CLASS-XII
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()
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

You might also like