Python - Database Applications
MySQL Database
MySQL should have been installed on your computer.
Install MySQL Driver
Python needs a MySQL driver to access the MySQL database - "MySQL Connector".
Use PIP to install "MySQL Connector". (Iinstalled in your Python environment)
Navigate command line to the location of PIP, and type the following:
Download and install "MySQL Connector":
C:\Users\Your Name\AppData\Local\Programs\Python\Python37\Scripts>python -m pip install mysql-
connector
Test MySQL Connector
To test if the installation was successful, or if you already have "MySQL Connector" installed, create a Python
page with the following content:
import [Link]
Object and Methods
MySQL Connector is a standardized database driver for Python and enables Python programs to access
MySQL databases.
Use the [Link]() method of MySQL Connector with required parameters to connect
MySQL.
Use the connection object returned by a connect() method to create a cursor object to perform Database
Operations.
The [Link]() to execute SQL queries from [Link] parameters found in the tuple are bound to
the variables in the operation. Specify variables using %s.
[Link]() Method sends a COMMIT statement to the MySQL server, committing the
current transaction.
[Link]() Method fetches all rows of a query result set and returns a list of tuples. If no more
rows are available, it returns an empty list.
Create Connection
Start by creating a connection to the database. Use the username and password from your MySQL database:
import [Link]
mydb = [Link](host="localhost", user="root", passwd=" ")
print(mydb)
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement:
……..
mycursor = [Link]()
[Link]("CREATE DATABASE school")
The MySQLCursor class instantiates objects that can execute operations such as SQL statements. Cursor objects
interact with the MySQL server using a MySQLConnection object. To create a cursor, use the cursor() method of a
connection object:
Check if Database Exists
You can check if a database exist by listing all databases in your system by using the "SHOW DATABASES" statement:
……..
mycursor = [Link]()
[Link]("SHOW DATABASES")
1 Prepared by Sisira Palihakkara
for x in mycursor:
print(x)
Creating a Table
To create a table in MySQL, use the "CREATE TABLE" statement. Make sure you define the name of the database
when you create the connection.
import [Link]
mydb = [Link]( host="localhost", user="root", passwd="", database="school" )
mycursor = [Link]()
[Link]("CREATE TABLE student (adno CHAR(3), name VARCHAR(15), grade int)")
Check if Table Exists
You can check if a table exist by listing all tables in your database with the "SHOW TABLES" statement:
……..
mycursor = [Link]()
[Link]("SHOW TABLES")
for x in mycursor:
print(x)
Insert Into Table
To fill a table in MySQL, use the "INSERT INTO" statement.
………
mycursor = [Link]()
sql = "INSERT INTO student (adno, name, grade) VALUES (%s, %s, %s)"
val = ("101", "Saman", 12)
[Link](sql, val)
[Link]()
print([Link], "Record inserted.")
Select From a Table
To select from a table in MySQL, use the "SELECT" statement:
……….
mycursor = [Link]()
[Link]("SELECT * FROM student")
myresult = [Link]()
for x in myresult:
print(x)
2 Prepared by Sisira Palihakkara