B.Sc. Hons.
Computing/Information System
ICT 1405 - Programming Methodologies
Python MySQL
<@Lanka Nippon BizTech Institute> 1
ACKNOWLEDGEMENT
Presented by : Ms. C. D. Padmaperuma
Ms. Hansika Samanthi
Contributors : Ms. Hansika Samanthi
Ms. C. D. Padmaperuma
Programming Methodologies | MultiThreading 2
REFERENCES
• Course Page
UOG 6: https://lms.lnbti.lk/course/view.php?id=851
UOG 11: https://lms.lnbti.lk/course/view.php?id=1306
• References available for the course
w3schools Python - https://www.w3schools.com/Python/
Real Python - https://realpython.com/
Programming Methodologies | MultiThreading 3
MySQL
• MySQL is a relational database management system based on the Structured
Query Language, which is the popular language for accessing and managing
the records in the database.
• MySQL is open-source and free software under the GNU license.
• It is supported by Oracle Company.
Python + MySQL
• Python is a high-level, general-purpose,
and very popular programming
language.
• Basically, it was designed with an
emphasis on code readability, and
programmers can express their
concepts in fewer lines of code.
• We can also use Python with SQL.
• The diagram given below illustrates
how a connection request is sent to
MySQL connector Python, how it gets
accepted from the database and how
the cursor is executed with result data.
Installing MySQL Server and MySQL
Connector/Python
• To connect MySQL and Python, it needs to set up two things:
• MySQL server
• MySQL connector.
• MySQL server will provide all the services required for handling your database.
• Once the server is up and running, you can connect your Python application
with it using MySQL Connector/Python.
Installing MySQL Connector/Python
• A database driver is a piece of software that allows an application to connect
and interact with a database system.
• Programming languages like Python need a special driver before they can
speak to a database from a specific vendor.
• These drivers are typically obtained as third-party modules.
• The Python Database API (DB-API) defines the standard interface with which
all Python database drivers must comply.
• In Python you need to install a Python MySQL connector to interact with a
MySQL database.
• Many packages follow the DB-API standards, but the most popular among them
is MySQL Connector/Python.
Establishing a Connection With MySQL Server
• MySQL is a server-based database management system. One server might
contain multiple databases.
• To interact with a database, you must first establish a connection with the
server.
• The general workflow of a Python program that interacts with a MySQL-based
database is as follows:
• Connect to the MySQL server.
• Connect to the existing database. ( Or database can be created newly)
• Execute a SQL query and fetch results.
• Inform the database if any changes are made to a table.
• Close the connection to the MySQL server.
Establishing a Connection
• Need connect() from the mysql.connector module.
• This function takes in parameters like host, user, and password and returns a
MySQLConnection object.
• You can receive these credentials as input from the user and pass them to
connect():
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(
host=“localhost",
user=“root”,
password=“1234",
database=“Company"
)
if connection.is_connected():
print("Connected to MySQL database")
except Error as e:
print("Error while connecting to MySQL:", e)
Create Database
• To create a database in MySQL, use the "CREATE DATABASE" statement:
create a database named "mydatabase":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost“,
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
Create a Table
Create a table named "customers":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address
VARCHAR(255))")
Insert Into Table
• To fill a table in MySQL, use the "INSERT INTO" statement.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
Select From a Table
• To select from a table in MySQL, use the "SELECT" statement:
Select all records from the "customers" table, and display the result:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password=
"yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Delete Record
• You can delete records from an existing table by using the "DELETE FROM" statement:
Delete any record where the address is "Mountain 21":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DELETE FROM customers WHERE address = 'Mountain 21'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
END
Programming Methodologies | MultiThreading 16