The document outlines the process of connecting Python to a MySQL database, detailing both frontend and backend roles in an application. It provides step-by-step instructions for establishing a connection, executing SQL queries, and managing data retrieval. Additionally, it emphasizes the importance of using placeholders in SQL queries for security and data handling purposes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views9 pages
Python SQL Connectivity
The document outlines the process of connecting Python to a MySQL database, detailing both frontend and backend roles in an application. It provides step-by-step instructions for establishing a connection, executing SQL queries, and managing data retrieval. Additionally, it emphasizes the importance of using placeholders in SQL queries for security and data handling purposes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
1. Introduction
2, Steps to connect
3. Examples
c
a
lg
a
Cc
c
q
q
c
va
rg
4
c
c
a
c
c
c
4G
c
co
pee Sra Ry‘INTRODUCTION
FRONTEND? a
What user sees and interacts with
Login Page, registration form etc.
BACKEND ?
Decides what to do with data, brain of your
application.
REQUIREMENTS ?
Python communicates with MySQL using a
connector / driver.
ip_install_ mysql-conn
STEPS ?
Start Python
Import connector
Establishing a connection with database
Create a cursor instance
Execute a query
Extract data from result set
Clean up the environmentSTEPS
1. Start Python
2. Import connector
Sn eee
Cn ee
3. Establishing connection with MySQL
~ connect() function of [Link]
establis n to MySQL databases.
Ea
oad
rd
ec
rere
: connect
oe
feos
password =
ore
##To check if connection is successful ?
pera eee
5 Cat
host =
a
Pree
datobase =
H
ena 0
pak
conn | ConnectionObject is further used to
create cursor, commit changes, close
connection
riot)4. Create a cursor instance
~ ADatabase cursor is used to process record
row by row, cursor( )is used in creation.
Cine
5. Execute SQL Query
+ executes )is used to execute SQL Query,
Cursor Object. ("SQL")
Et ogee)
To execute multiple sql queries
cur. ea)
Whenever we use INSERT, UPDATE, DELETE
is mandatory
Extract Data
fetcholl( ): Returns all records in tuple form
Fetchonel): Returns one record as tuple, IF no
record, it reutrns None.
fetchmanyl em} Returns n records in alist of
tuples, IF no record it returns None.
rowcount: Property of cursor, Returns no of rows
retrieved from the cursor
Clean up the environment
After all the processing close the connection,ene ers
ee eceeccas
coe 7
eee
eo
Creer)
omen
Ease
een
ete)
Pesan
eet
oo)
pret)
conn. close()
=password=
oe
arene)
eon
[Link]()
eee)ers
Cee en cca?
i
ee
ee
renee)
Pee
Pereeenta)
Peeeere
eee attr
print (row)
Eres)
eee
RN)# Note
~ When we write SQL queries in Python, we
should NOT directly insert values into the
query string.
~ Instead, we use placeholders like ‘ss and pass
values separately.
Why?
~ Security (SQL Injection se bachata hai)
~ Automatic data handling (numbers, strings)
~ Professional & recommended methodaerate Ten
con. commit ()
‘sql = “DELETE FR
emer a
Pemereatss)
sal c
Peet)