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

Final Revised MySQL Connectivity

Uploaded by

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

Final Revised MySQL Connectivity

Uploaded by

r.unknown457
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Know Python Bytes

www.knowpythonbytes.blogspot.com

PYTHON-MYSQL
CONNECTIVITY

PYTHON PROGRAM FROM


WHERE YOU WILL ACCESS THE
DATABASES CONTAINING
DATABASE AND TABLES
TABLES (BACK END SIDE)
(FRONT END SIDE)

Mrs. Payal Bhattacharjee


PGT(C.Sc.)
K V No. I Kanchrapara
CONTENTS ( Learning Outcomes ) ☺
XII CSc 2021-22
In TERM-2

UNIT-3 DATABASE MANAGEMENT


(Sub-topic)
INSTALLING MYSQL-CONNECTOR-PYTHON
Pre-requisites:- Commonly, Python applications will need to access
a database of some sort.
1 . Install Python  The Python standard for database interfaces is the Python
DB-API. Most Python database interfaces adhere to this
2. Install MySQL standard.
 You must download a separate DB API module for each
3. Open Command prompt database

4. Switch on internet connection MySQL alone has the following interface modules
to choose:
5. On the command prompt type  MySQL for Python (import MySQLdb)
 MySQL Connector/Python (import mysql.connector)
pip install mysql-connector-python etc

and execute. But, remember to enter Download and install MySQL Connector/Python
inside the Python scripts folder before  https://dev.mysql.com/downloads/connector/python/

typing this on command prompt. what is pip?


pip is a packoage manager for Python. Pip stands
6. Open python IDLE
for "Pip Installs Packages" or "Pip Installs Python".
7. import mysql.connector Alternatively, pip stands for "preferred installer
program“. Pip is basically a tool that allows you
mysql-connector-
python-2.1.7- to install and manage additional libraries and
Remember to check your Python py3.4-windows- dependencies that are not distributed as part of
version x86-64bit.msi
the standard library.
Successfully installed

OPEN IDLE AND TYPE


THE FOLLOWING
import statement.
If the cursor simply
returns without any
error to the
>>>Python prompt
,that indicates that
the package has been
successfully installed.
STEPS FOR CREATING DATABASE CONNECTIVITY APPLICATIONS
Step-1 Start Python
Step-2 Import the package required Step-2
import mysql.connector
for database programming OR
import mysql.connector as MC
Step-3 Open a connection to database
MYCON=mysql.connector.connect(host=my_host,user=my_user,password=my_password,database=my_DB
OR
MYCON=MC.connect(host=my_host,user=my_user,password=my_password,database=my_DB
Step-4 Create a cursor instance CUR=MYCON.cursor() Get Resultset from the
MySQL table i.e. all
Step-5 Execute a query CUR.execute(“Select * from Student”) those records which
satisfies the Query
Step-6 Extract data from result set using functions statement given in the
execute() method
viz.[fetchall(),fetchone(),fetchmany(),rowcount()]
Step-7 Clean up the environment MYCON.close()
USEFUL METHODS USED IN THE MYSQL-PYTHON
CONNECTIVITY PROGRAMS
What connect() method does in MySQL Python connectivity?
To create a connection between the MySQL database and the python application, the connect() method of
mysql. connector module is used. Pass the database details like HostName, username, and the database
password in the method call. The method returns the connection object.
What is cursor in MySQL Python connector?
The MySQLCursor of mysql-connector-python (and similar libraries) is used to execute statements to
communicate with the MySQL database. Using the methods of it you can execute SQL statements, fetch data
from the result sets, call procedures.
What does cursor execute do in Python?
A cursor is an object which helps to execute the query and fetch the records from the database.
What is the role of Execute () in MySQL Python connectivity?
This method executes the given database operation (query or command). The parameters found in the tuple
or dictionary are bound to the variables in the operation. NOTE: In Python, a tuple containing a single value
must include a comma.
What is commit () in Python?
The commit() method is used to make the database transactions. The commit() method is used to confirm
the changes made by the user to the database. Whenever any change is made to the database using update
or any other statements, it is necessary to commit the changes.
How do you close a connection and cursor in Python?
The close() method closes the cursor, resets all results, and ensures that the cursor object has no reference
to its original connection object.
#CONNECTIVITY CHECKING

is_connected() is used to Before we can access data in a


check whether Python- database, we must open a connection
MySQL connectivity is
successful or not to the MySQL server.
The connect() method creates a
connection to the MySQL server
and returns a Connection object.

OUTPUT ON THE SHELL


OUTPUT ON THE SHELL
1

OUTPUT ON THE SHELL

3
FETCH DATA FROM DATABASE
Multiple ways to retrieve data:
 fetchall()
 Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a
List of Tuples). Fetchall( ) returns all the records/rows from the result set in the form of a
sequence.
 fetchmany(size)
 Fetch the next set of rows of a query result, returning a sequence of sequences. It will return
number of rows that matches to the size argument. fetchmany() returns a list of
tuples. If no more rows are available, it returns an empty list.
 fetchone()
 it returns only one record/row from the result set. i.e. first time it will return first record,
next time it will return second record and so on. fetchone() returns a single
sequence, or None if no more rows are available
▪ rowcount()
▪ The rowcount is a property of cursor object that returns the number of rows retrieved from the
cursor so far. Rowcount() returns the number of rows affected by the
last execute method for the same cur object and thus it returns -1
if no execute() method is done prior to that. i.e. the result set has not
been created yet.
OUTPUT ON THE SHELL
OUTPUT ON THE SHELL

Fetchall() method is returning a LIST [ ]


of Tuples () i.e.

A Sequence(LIST) of Sequences(Tuples)
OUTPUT ON THE SHELL
OUTPUT ON THE SHELL
BEFORE EXECUTING THE PYTHON CODE AFTER EXECUTING THE PYTHON CODE

The format() method formats the specified value(s)


and insert them inside the string's placeholder.
The placeholder is defined using curly brackets {}.
The format() method returns the formatted string.

OUTPUT ON THE SHELL


Here, rec is a LIST.

OUTPUT ON THE SHELL


OUTPUT ON THE SHELL
OUTPUT ON THE SHELL
BEFORE EXECUTING THE PYTHON CODE
Record with admno=105 is present.

OUTPUT ON THE SHELL

AFTER EXECUTING THE PYTHON CODE


Record with admno=105 has been deleted.
BEFORE EXECUTING
THE PYTHON CODE

AFTER EXECUTING
THE PYTHON CODE

OUTPUT ON THE SHELL


REFERENCES AND BIBLIOGRAPHY
ACKNOWLEDGEMENT

https://dev.mysql.com
https://google.com
And other websites

XII Computer Science books


→Kips Publication
→Dhanpat Rai Publisher
Stay safe. Stay aware. Stay healthy. Stay alert.

You might also like