VKS-Interface Python With MySQL
VKS-Interface Python With MySQL
on
Every application required data to be stored
for future
reference to manipulate data. Today every
Fo exampl reservati syste stores
application
details
rstores for reserving
e, data the
inondatabase seats
mfor and later on
passengers
this purpose
for sending some messages or for printing
tickets
I scho etc.
stude detail ar save fo man reaso
like
n olattendance,
nt s fee collections,
e d r exams,
y ns
report
Pythoncard etc.us to connect all types of
allows
database like
Oracle, SQL Server, MySQL.
In our syllabus we have to understand
how to connect
Python programs with MySQL
Pre-requisite to connect Python w
MySQL
Before we connect python program with
any database
like MySQL we need to build a bridge to
To build this bridge so that data can travel
connect
weboth
Python need
andaMySQL.
connector called
ways
“mysql.connector”.
We ca inst “mysql.conne b usin followi
methon all ctor” y g ng
ds:
At command prompt (Administrator login)
Type “pip install mysql.connector” and press
enter
(internet connection in required)
This connector will work only for MySQL 5.7.3 or
later
Or open OS and Python
“https://dev.mysql.com/downloads/
version
connector/python/”
Connectin MySQL Pyth
g to from on
Once the connector is installed you are
ready to
connect your python program to
MySQL.
The following steps to follow while
connecting your python program with
MySQL
Open python
Import the package required (import
mysql.connector)
Open the connection to database
Create a cursor instance
Execute the query and store it in resultset
Extract data from resultset
Importing
mysql.connector
impo mysql.conn O Import MYSQLdb
rt ector r
O
r
import
mysql.connector
as ms Import MYSQLdb as ms
is_connected() function
returns
true connection is
if established
otherwise false
Tabletowork
(emp)
Creating
Cursor
It is a useful control structure of database
connectivity.
When
resultswe
(sefire a query to
of records) sedatabase, it is
ove th connect i
executed
et t and is nt r e ion n
one
We may want to access data one row at a
go. time, but
query cannot happens as one row at
processing a time,
so cursor in performing this task.
help us Cursor stores
all the data temporary container of
as a returned data
and we can data one row at a time from
Creating Cursor and Quer
Executing y
TO CREATE CURSOR
Cursor_name =
connectionObject.cursor()
For e.g.
mycursor = mycon.cursor()
TO EXECUTE QUERY
t connect
We use execute() function to o ion
send query
Cursor_name.execute(query)
For e.g.
mycursor.execute(„select * from
emp‟)
Exam - Curs
ple or
le ng a
AFTER PROGRAM
EXECUTION
Examp Updati reco
le: ng rd