Python has good MySQL support, via multiple packages.
In this course we will be using the MySQLdb package to connect
from Python to a MySQL database server and run queries.
For more information on MySQLdb see the
official documentation.
The code to connect to the database is rather straight forward:
import MySQLdb
db = MySQLdb.connect('hostname', 'username', 'password', 'databasename')
db.autocommit(True)
To disconnect, when all queries are completed, usually at the end of
your Python script:
db.close()
In practice, this method is not what is normally used
directly. Please review the next section!
Since your username and password (for the database) is needed for the connection, we'll create a small mydb.py script which stores the connection and can be used by all of our scripts that need the connection. As an exercise for this lecture we'll create a file and a connection class to work with the database. The example below is in a file called mydb.py:
import MySQLdb
db = MySQLdb.connect('db.cs.usna.edu', # Hostname
'm123456', # Username
'm123456', # Password
'm123456') # Schema/DB Name
db.autocommit(True)
and then connect to the database via:
from mydb import db # connect to MySQL when needed
There are a few basic steps needed when performing SQL queries via Python and MySQL. In the examples below simple queries will be presented on the left, while dynamic queries are shown on the right.
cursor = db.cursor()
query = """SELECT Alpha, Course
FROM Grades
WHERE Course='SD321'"""
query = """INSERT INTO Grades(Alpha, AcYear,
Sem, Course, Title)
VALUES(250234, 2024, 'FALL', 'SD321', 'Data Storage')"""
query = """SELECT Alpha, Course
FROM Grades
WHERE Course=%s"""
%s where written in the SQL query string.
values = ['SD321']
cursor.execute(query)
cursor.execute(query, values)
warning = cursor.messages
insert_id = cursor.lastrowid # auto_increment value last inserted
row_count = cursor.rowcount # number of rows affected by last insert, delete, or update
column_info = cursor.description # description of columns selected
cursor.fetchone(),cursor.fetchmany(size = 10),cursor.fetchall() (this can be a problem if the result
set is very large and does not fit in memory).
Below are a few examples of retreiving the relevant rows using the methods above.
# Example 1: Using a loop and fetchone()
row = cursor.fetchone()
while row is not None:
print(row)
row = cursor.fetchone()
# Example 2: Using the cursor as iterator
for (alpha, course) in cursor:
print(f'Student {alpha} in course {course}')
# Example 3: Using fetchall
allresults = cursor.fetchall()
for row in allresults:
print(row)
cursor.close()
If you don't have the Alpha and Grades tables from previous classes or labs, please download the sample data - sample_data.sql.
connection with a constructor that
accepts username, password, host, and database as parameters and connects
the specified database on the server, which will be stored in self.db property of the
class. query (see the "Performing a
basic query" section) and accept a sql
statement and a list of values as parameters. The query method
should execute the provided query and return
the list of names of the columns in the result,
the actual data returned by the query,
the value of the last row id inserted,
any errors produced by the query,
and any warnings. close that closes the database connection.db that is an object of type connection,
initialized with corect values
to connect to your own database.db object from mydb.
Submit a select query, and print
the Alpha, First, Last, Course, and CourseGrade for students in Midn and
Grades tables.db object from mydb. The script
should retrieve an alpha, first, last, connect to the database, insert
a row into the Midn table with the information received from the user, and print
whether the insert was successful or not.