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

Lecture on Interface of Python with an SQL Database (2)

Uploaded by

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

Lecture on Interface of Python with an SQL Database (2)

Uploaded by

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

LECTURE ON

CONNECTING MYSQL
WITH PYTHON
BY: ABHISHEK BHARDWAJ
PGT- COMPUTER SCIENCE
Connecting Python with MySQL

 Python can be used in database applications.


 One of the most popular databases is MySQL.
Connecting Python with MySQL

 In order to create a database connectivity between Python and


MySQL, we first need to install MySQL connector.
 MySQL connector is used to connect Python with MySQL, so
that, we can store/ retrieve the data from Python as front-end
and MySQL as back-end.
Setting MySQL Path : Before installing MySQL, we must ensure
the following :
 Python is installed and computer is connected with Internet.
 Path must be set for Python in System’s Environment Variable.
This is done, when, we install Python and tick check box to add
Python to Path. (See Figure on Next Slide)
 Check Path by opening Command Prompt and type Python and
press ENTER key. (See Figure on Next Slide)
Connecting Python with MySQL
Connecting Python with MySQL

Setting Path for Python : If path for Python is not Set?


Option 1 : Uninstall and again install Python by checking the
check box “Add Python 3.7 to PATH” at installation time.
Option 2 : Click on start button and type python.exe.
 Right click on the icon and click on “Open File Location”.
 Now again right click on address bar and “copy address” e.g.
C:\Users\Admin\AppData\Local\Programs\Python\Python36-32
 We may copy the path by opening IDLE, click on File Menu, click
on Save As option.
 By default the location in window, which, pop-up indicates the
Path. Right Click on it and Copy Path.
 Now open Environment Variable by right click on :
This PC icon >>> Properties >>> Advanced System Settings >>>
Environment Variables >>> Path
Connecting Python with MySQL

 Click on Path then click Edit, click on New.


 Paste the copied path.
 Again click on New and again paste the copied path by adding
“\Scripts\” in the end of path.
 Now click OK until we exit from all pop-up windows.
 Check Path by opening Command Prompt and type Python and
press ENTER key.
Connecting Python with MySQL

Installing MySQL Connector/ Driver to connect with Python :


 Click on start button and type python.exe.
 Right click on the icon and click on “Open File Location”.
 Now search for Scripts Folder and open it.
 Click on address bar and select the address. Press Delete Key
and type “cmd”, press Enter.
 The Command Prompt window will appear with same path, on
which we typed “cmd”.
 Now ensure your PC is connected with internet.
 Type on Command Prompt– pip install mysql-connector-python
 Connector will be installed successfully after pressing Enter Key.
(See image on Next Slide)
Connecting Python with MySQL

1. pip is the package installer for Python.


2. Use pip to install packages from the Python Package Index and other indexes.
3. pip is a recursive acronym that can stand for either “pip Installs Packages" or “pip
Installs Python”
Connecting Python with MySQL

Test MySQL Connector :


 Open IDLE
 Type import mysql.connector

No Error is Shown, It means connection is successful.


Connecting Python with MySQL

Test MySQL Connector : Write a code snippet to Check MySQL


connection with Python.
Code Snippet

Output
Inserting Data into MySQL Table
(MySQL Connectivity)
Steps :
1. Open MySQL prompt.
2. Create Database or use an existing Database.
3. Create a New Table, if table does not exist in the selected
Database.
4. Write code in front-end language (Python) to connect, and
update data in back-end (MySQL).
5. A cursor is an object which helps to execute the query and
fetch (scoop/bring) the records from the database.
Inserting Data into MySQL Table
(MySQL Connectivity)
Write a program in Python to insert a record in MySQL Table.
Code Snippet :

A cursor is an object which helps to execute


the query and fetch (scoop/bring) the Records
from the database.
Inserting Data into MySQL Table
(MySQL Connectivity)
Write a program in Python to insert a record in MySQL Table.
Inputs : Output :
Inserting Data into MySQL Table
(MySQL Connectivity)
Write a program in Python to insert records in MySQL Table.
Code Snippet :
Inserting Data into MySQL Table
(MySQL Connectivity)
Write a program in Python to insert records in MySQL Table.
Inputs : Output :
Updating Data into MySQL Table
(MySQL Connectivity)
Write a program to update MySQL data through Python Code.
Code Snippet :

ROW_COUNT() returns the number of


rows updated, inserted or deleted by
the preceding statement. This function
is used to check, whether, record
updated? or record not found? if user
enters wrong Sch_No or commits
wrong entry.
Updating Data into MySQL Table
(MySQL Connectivity)
Write a program to update MySQL data through Python Code.
Inputs : Output :
Extracting Data from MySQL Table
(MySQL Connectivity)
Extracting Data from Database : To extract data from the table
there are three functions provided by Python.
1. fetchone() : Returns a single record or none if no more rows
are available.
2. fetchmany(size) : Returns the number of rows specified
by 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 returns an
empty list. Default value of size argument is 1.
3. 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.
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchone()

1. Returns the First Record/Row as result.


2. Use WHERE clause, to fetch a particular
record/ row.

rowcount function returns number of fetched rows. This function don’t have
braces ()
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchone()
Inputs : Output :
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchone() for fetching consecutive records.
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchone() for fetching consecutive records.
Inputs : Output :
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchmany() for fetching records from MySQL.

If size argument in fetchmany(size) is > no. of records/rows in table, then,


it returns all available records/ rows from a table.
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchmany() for fetching records from MySQL.
Inputs : Output :
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchall() for fetching records from MySQL.
Extracting Data from MySQL Table
(MySQL Connectivity)
Implementation of fetchall() for fetching records from MySQL.
Inputs : Output :

You might also like