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

Day 1 - 12 A C Ls - 8 - Interface Python With SQL 2024 - 2025

f

Uploaded by

neer028849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Day 1 - 12 A C Ls - 8 - Interface Python With SQL 2024 - 2025

f

Uploaded by

neer028849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1

Ls – 8
Interface Python with SQL
SYLLABUS:
• Interface of Python with an SQL database
• Connecting SQL with Python
• Performing Insert, Update, Delete queries using cursor
• Display data by using fetchone(),fetchall(),rowcount
• Creating Database connectivity Applications

Learning Objective : To interface Python with MySQL


2

STEPS TO ESTABLISH DATA CONNECTIVITY


1. Import package for mysql connectivity → import mysql.connector as ms
2. Connect Python to MySQL dB → mydb=ms.connect(host='localhost',
user='root',
password='12345',
database=‘<db_name>')

3. Create cursor instance → mycursor=mydb.cursor()


4. Execute query → mycursor.execute(‘show databases’)
5. Extract data → mycursor.fetchall() / mycursor.fetchone() / mycursor.fetchmany(n)
(NB : To know the no of records fetched - mycursor.rowcount )
6. Terminate connection → mydb.close( )

Learning Objective : To interface Python with MySQL


3

1. Import MySQL Connector


➢ Import the MySQL Connector python module to your program, so that you can
use the modules API to connect MySQL.
import mysql.connector
Or
import mysql.connector as ms
NB : ms is an alias, so every time we can use “ms” in place of mysql.connector

Learning Objective : To interface Python with MySQL


4

2. Connect Python to MySQL database


mysql.connector.connect( ) OR ms.connect()
➢ Create a connection object (mydb), once the connectivity is established to the database in the
host through the correct user and password, by using the connect( ) method.

mydb=ms.connect(host='localhost', user='root', password='12345',database=‘<db_name>')

passwd /password
Write from
next page

NB:
➢ connect ( ) function establishes a connection to the MySQL database from Python Application and returns a
MySQLConnection Object (mydb).
➢ This MySQLConnection Object (mydb) is used to perform various actions on the Database.
➢ To know the current user ; check in mysql >select current_user();

Learning Objective : To interface Python with MySQL


5

connect( ) function establishes Database name to which


connection to db & returns a connection is to be
Connection Object established.

MySQL Connection Object Host Name – the server name or IP address on which
Used to perform actions on MySQL is running
the Database. Username – the username that you use to work with MySQL
Server (Default : root)
Password – Password is given by the user at the time of
installing the MySQL database .

Learning Objective : To interface Python with MySQL


6

3. Create cursor Object (instance)


<connection>.cursor( )
➢ This method returns a cursor object, using which we execute the SQL queries.
➢ Cursor object interacts with MySQL server using the MySQLConnection Object.
Eg:
mycursor=mydb.cursor()

Learning Objective : To interface Python with MySQL


7

4. Execute Query - cursor().execute( )

➢ cursor( ) is a control statement to traverse over records in a database. It is used to fetch results of the
query.
➢ Cursor is used to traverse the records from the result set.
➢ Cursor stores all the data as a temporary container of returned data and we can fetch data one row at a time
from Cursor.
➢ An arbitrary number of cursors can be created.
➢ execute( ) function is used to send SQL query to a connection.
cursor_name.execute(query)
Eg: - mycursor.execute(‘show databases’)

Learning Objective : To interface Python with MySQL


8

5. Fetching(extracting) data from ResultSet


To extract data from cursor following functions are used:
➢ cursor.fetchall() : Returns all the record in the form of tuple.
➢ cursor.fetchone() : Returns 1 record from the result set.
➢ cursor.fetchmany(n) : Returns ‘n' number of records.
➢ rowcount : it will return number of rows retrieved from the cursor so far.

Learning Objective : To interface Python with MySQL


9

6. Terminate connection to db
➢ connection.close()
• To terminate the MySQL database connection.

Learning Objective : To interface Python with MySQL


10

TO CHECK CONNECTIVITY is_connected()

Learning Objective : To interface Python with MySQL

You might also like