Chapter 14 Interface Python With Mysql

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CHAPTER 14: INTERFACE PYTHON WITH MYSQL

In order to connect to a database from within Python, we typically use a library such as mysql-connector-
python or PyMySQL. These libraries provide the functionality to connect to a MySQL database, execute SQL
queries, and retrieve data. We will work with mysql connector library for the same in this chapter.
CONNECTING TO MYSQL FROM PYTHON
In order to connect Python with MySQL, we first need to install the mysql-connector-python library. Use
the following command to install the library.

pip install mysql-connector-python

STPES FOR CREATING DATABASE CONNECTIVITY APPLICATIONS


There are mainly seven steps that must be followed in order to create a database connectivity application.
1. Start Python
2. Import the packages required for database programming
3. Open a connection to database
4. Create a cursor instance
5. Execute a query
6. Extract the data from result set
7. Clean up the environment
#1. START PYTHON
Begin by setting up your Python environment. Open a Python IDE or terminal where you will write and
execute your database connectivity code. It can be IDLE or Spyder IDE, whatever you feel comfortable in.
#2. IMPORT THE PACKAGES REQUIRED FOR DATABASE PROGRAMMING
First of all you need to import mysql.connector package in your Python scripts by using the command:

import mysql.connector
or
import mysql.connecotr as sqLtor

#3. OPEN A CONNECTION TO THE MYSQL DATABASE


In the next step, we need to establish the connection to the MySQL database using connect() function of
mysql.connector package.
Example Code:

import mysql.connector
connection = mysql.connector.connect(
host="localhost", # The hostname or IP address of the database server
user="your_username", # Username for database authentication
password="your_password", # Password for the specified username
database="your_database" # Name of the database to connect to
)
Parameters Required
The connect() function requires four parameters which are:

 Host: The address of the database server. Use “localhost” if the database is on the same machine.
 User: The username to log into the database.
 Password: The password corresponding to the username.
 Database: The specific database you want to connect to. It is optional and If omitted, the
connection is established without selecting a default database.
Connection Object
The connect() method returns a connection object that acts as the communication link with the database.
Connection Validation
We can check for the successful connection using the function is_connected() with the connection object,
which return True, if the connection is successful and false if the connection is not successful.

if connection.is_connected():
print("Connection successful")
else:
print("Connection failed")

#4. CREATE A CURSOR INSTANCE

The cursor is a fundamental component in database programming. After establishing a connection to the
database, creating a cursor allows the Python program to interact with the database by executing SQL
queries and retrieving data. Here is how it works.

Obtaining a Cursor Object

The connection object provides a cursor() method to create a cursor instance.

cursor = connection.cursor()

Here:

 connection is the object returned in Step 3.


 cursor is now the object through which queries are executed.
#5. EXECUTE SQL QUERY
Executing a query is one of the most important steps in database connectivity. After creating a cursor in
Step 4, the cursor is used to execute SQL commands to interact with the database.
Using the execute() method
The cursor.execute() method sends a single SQL query to the database for execution. Example:

cursor.execute("SELECT * FROM employees")

The above code will execute the given SQL query and store the retrieved records (i.e., the resultset) in the
cursor object (namely cursor) which you can then use in your programs/scripts as required.
#6. EXTRACT DATA FROM RESULTSET
After executing a query (especially a SELECT query), the database returns a result set. This step focuses on
extracting and processing the data retrieved from the database using various cursor methods.
Result Set

 A result set in the collection of rows returned by a query.


 The cursor object provides methods to fetch data from this result set.
Methods for Extracting Data
Once the result of query is available in the form of a resultset stored in a cursor object, you can extract
data from the resultset using any of the following fetch…() functions.

 fetchone(): Fetches a single row from the result set. If no more rows are available, it returns None.
row = cursor.fetchone()
print(row)
 fetchmany(size): Fetches the specified number of rows as a list of tuples.
rows = cursor.fetchmany(3)
for row in rows:
print(row)
 fetchall(): Fetches all remaining rows in the result set as a list of tuples.
rows = cursor.fetchall()
for row in rows:
print(row)

Iterating Through Results


You can iterate through the cursor directly for row-by-row processing:

for row in cursor:


print(row)

Full Example with Employee Table


Employee Table: Structure and Data

ID Name Age Department Salary


1 Alice 30 HR 50000
2 Bob 35 Finance 60000
3 Carol 28 IT 55000
4 David 40 Marketing 70000
5 Emma 25 Sales 45000
Python code to extract Results using fetchone(), fetchmany(), and fetchall()

import mysql.connector
# Establish connection to the MySQL database
connection = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="dbname"
)
# Create a cursor instance
cursor = connection.cursor()

# Execute a SELECT query


cursor.execute("SELECT * FROM employees")

# Fetch data using fetchone()


print("Using fetchone():")
row = cursor.fetchone()
while row:
print(row)
row = cursor.fetchone() # Fetch the next row

# Re-execute the query for the next method


cursor.execute("SELECT * FROM employees")

# Fetch data using fetchmany()


batch_size = 2 # Number of rows to fetch per batch
print("\nUsing fetchmany() with batch size", batch_size, ":")
while True:
rows = cursor.fetchmany(batch_size)
if not rows: # Break the loop when no more rows are available
break
for row in rows:
print(row)
# Re-execute the query for the next method
cursor.execute("SELECT * FROM employees")

# Fetch data using fetchall()


print("\nUsing fetchall():")
rows = cursor.fetchall()
for row in rows:
print(row)

# Close the cursor and connection


cursor.close()
connection.close()

Output

Using fetchone():
(1, 'Alice', 30, 'HR', 50000)
(2, 'Bob', 35, 'Finance', 60000)
(3, 'Carol', 28, 'IT', 55000)
(4, 'David', 40, 'Marketing', 70000)
(5, 'Emma', 25, 'Sales', 45000)
Using fetchmany() with batch size 2 :
(1, 'Alice', 30, 'HR', 50000)
(2, 'Bob', 35, 'Finance', 60000)
(3, 'Carol', 28, 'IT', 55000)
(4, 'David', 40, 'Marketing', 70000)
(5, 'Emma', 25, 'Sales', 45000)
Using fetchall():
(1, 'Alice', 30, 'HR', 50000)
(2, 'Bob', 35, 'Finance', 60000)
(3, 'Carol', 28, 'IT', 55000)
(4, 'David', 40, 'Marketing', 70000)
(5, 'Emma', 25, 'Sales', 45000)
#7. CLEAN UP THE ENVIRONMENT
After you are through all the processing, in this final step, you need to close the connection established as
follows:

connection.close()

CONNECTING WITH MYSQL DATABASE USING PYMYSQL


To connect Python with MySQL database, so far we have used mysql.connector for this, but we can also
use pymysql library for connecting with a MySQL database.
The basic difference between mysql.connector and pymysql is that the mysql.connector is made available
by Oracle, which owns MySQL, and the pymysql library is made available by Python(It is pure Python).
#STEP1: START PYTHON
To start Python, simply open your Python environment or IDE (such as IDLE, PyCharm, or VSCode) and
ensure Python is installed and running properly on your system.
#STEP2: IMPORT THE PACKAGES REQUIRED FOR DATABASE PROGRAMMING
To work with MySQL databases in Python, you need to import the pymysql package. If you don’t have it
installed, you can install it using pip:

pip install pymysql

Once installed, you can import it in your Python script:

import pymysql

#STEP3: OPEN A CONNECTION TO THE DATABASE

Now that you have the pymysql package, the next step is to establish a connection to the MySQL database.
You need to provide the following details to connect:

 host: The server where your MySQL database is running (often localhost or 127.0.0.1 for local
databases).
 user: The username used to log into the MySQL database.
 password: The password associated with the MySQL user.
 database: The specific database you want to connect to.

Here’s how you can establish the connection:

connection = pymysql.connect(

host='localhost', # or use an IP address if connecting remotely

user='your_username', # MySQL username

password='your_password', # MySQL password

database='your_database' # The database name to use

)
Example Code

import pymysql
# Step 3: Open connection to the MySQL database
try:
connection = pymysql.connect(
host='localhost',
user='your_username',
password='your_password',
database='your_database'
)
print("Connection successful!")

except pymysql.MySQLError as e:
print("Error while connecting to MySQL", e)

Rest of the steps, i.e., steps 4, 5, 6, 7 are identical to what you have learnt earlier in the previous section.
Note that connection object created using pymysql does not provided is_connected() method to the test
connection. Thus we shall not use it with pymysql.
PARAMETERISED QUERIES
Sometimes, we may need run queries which are based on some parameters or values that your provide
from outside, e.g.,

inp=70
SELECT * FROM student WHERE MARKS>inp;

Such queries are called parameterised queries.


Let us now learn about how we can form parameterised query strings. To form query strings based on
some parameters. You can go for one of the following two methods:
#1 OLD STYLE: STRING TEMPLATE WITH % FORMATTING
In this style, string formatting uses this general form: f%v
where,

 f is a template string
 v specifies the values to be formatted using the template. v must be a tuple.
 “Select * from student where marks > %s” % (70,)
#2 NEW STYLE: STRING TEMPLATE WITH { } FORMATTING
In Python, new-style string formatting refers to the method introduced in Python 2.7 and Python 3.0 using
curly braces {} and the str.format() method. This method is more flexible and powerful than the old %
formatting style. It allows placeholders within the string to be replaced by values passed into the format()
method, making string formatting cleaner and more readable.
Syntax:

"String with {placeholders}".format(value1, value2, ...)

 {}: Curly braces {} are used as placeholders for the values that will be inserted into the string.
 .format(): The format() method is used to bind the placeholders to the values provided.

Example
Table Student: Structure and Data

RollNo name Makrs stream


1 Neha 99 Commerce
2 Nisha 100 Commerce
3 Pooja 90 Arts
4 Priyanka 80 Arts
5 Soniya 50 Science
6 Swati 90 Commerce

Python code
Old Style Example

import mysql.connector
con=mysql.connector.connect(host="localhost",
password="root",
user="root",
database="school")

cur=con.cursor()
m=int(input("Enter marks:"))
s=input(“Enter stream:”)
query="Select * from student where marks>%s and stream=’%s’" % (m,s)
cur.execute(query)
data=cur.fetchall()
for i in data:
print(i)
New Style

import mysql.connector
con=mysql.connector.connect(host="localhost",
password="root",
user="root",
database="school")

cur=con.cursor()
m=int(input("Enter marks:"))
query="select * from student where marks >{} and stream = '{}'".format(m,s)
cur.execute(query)
data=cur.fetchall()
for i in data:
print(i)

PERFORMING INSERT AND UPDATE QUERIES


Since INSERT and UPDATE are also SQL commands, you can execute them just the way you have executed
SELECT queries earlier using the cursor.execute() method.
One important thing you must ensure with INSERT and UPDATE queries (which make changes to the
database unlike SELECT) you must commit your query after executing INSERT and UPDATE queries.
INSERT QUERY EXAMPLE

import mysql.connector
con=mysql.connector.connect(host="localhost",
password="root",
user="root",
database="school")
cur=con.cursor()
r=int(input("Enter roll no:"))
n=input("Enter name:")
m=int(input("Enter marks"))
s=input("Enter stream:")
query="Insert into student values ({}, '{}', {}, '{}')".format(r,n,m,s)
cur.execute(query)
con.commit()
print(“Row inserted successfully…..”)

UPDATE QUERY EXAMPLE

import mysql.connector
con=mysql.connector.connect(host="localhost",
password="root",
user="root",
database="school")

cur=con.cursor()
query="Update student set marks=50 where RollNo=3"
cur.execute(query)
con.commit()
print("Row updated successfully....")

You might also like