MCS-201 · Unit 16

Data Access Using Python

Section 4 of 7

Block 4 · Unit 16Data Access Using Python10 min · Section 4 of 7

16.4Connecting Python to MySQL

Covers IGNOU MCS-201 §16.3

Python talks to databases through driver modules that follow a common standard, so the steps are almost the same for MySQL, PostgreSQL or SQLite. This section sets up MySQL access and makes the first connection.

The Python DB-API

PEP 249, the Python Database API Specification v2.0, defines a common interface that database drivers follow:

The DB-API workflow
import driver  →  connect()  →  cursor()  →  execute(SQL)  →  fetch...() / commit()  →  close()
Object or methodPurpose
connect(...)Opens a connection to the database server; returns a connection object
connection.cursor()Creates a cursor, the object that sends SQL and holds results
cursor.execute(sql, params)Runs one SQL statement
cursor.fetchone(), fetchmany(n), fetchall()Read result rows
connection.commit(), rollback()Keep or undo the current transaction
cursor.close(), connection.close()Release resources

Installing what you need

  1. MySQL Server: install MySQL Community Server (the current long-term-support release is 8.4) from dev.mysql.com, and set a root password during setup. MySQL Workbench is an optional graphical tool.
  2. The driver: pip install mysql-connector-python (or conda install -c conda-forge mysql-connector-python in Anaconda).
  3. Check: in Python, import mysql.connector should run without error.

Connecting and creating a database

mysql.connector.connect() takes keyword arguments; the common ones are host (the server’s address, "localhost" for your own computer), user, password (passwd is an older alias), and optionally database (the database to use) and port (default 3306).

Key takeaways

  • PEP 249 (DB-API 2.0) gives all Python database drivers the same pattern.
  • Install MySQL Server and mysql-connector-python.
  • connect() → cursor() → execute() → fetch or commit → close().
  • Pass the database name as a string; keep passwords out of shared code.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which command installs the official MySQL driver for Python?

Show answer

Answer: C. pip install mysql-connector-python

mysql-connector-python is Oracle’s maintained package; mysql-connector is an outdated unofficial one.

Question 2 · multiple choice

Which object is used to execute SQL statements?

Show answer

Answer: B. The cursor

conn.cursor() returns a cursor whose execute() method sends SQL.

Question 3 · multiple choice

Which method opens a connection to MySQL?

Show answer

Answer: A. mysql.connector.connect()

connect() returns a connection object; cursors are created from that object.

Question 4 · multiple choice

What is wrong with mysql.connector.connect(host="localhost", user="root", password="pw", database=college)?

Show answer

Answer: C. college should be the string "college"

Without quotes, Python looks for a variable named college and raises NameError if none exists.

Question 5 · multiple choice

What is PEP 249?

Show answer

Answer: D. The Python Database API specification that database drivers follow

It defines connect, cursors, execute, fetch methods, commit and rollback for all drivers.

Question 6 · short answer

Fill in the blanks (Check Your Progress 16.5, Q2): mydb = mysql.______.connect(host="localhost", ______="myusername", passwd="mypassword"), mycursor = mydb.______(), mycursor.______("CREATE DATABASE mydatabase").

Show worked answer

connector, user, cursor, execute. The full code is mydb = mysql.connector.connect(host="localhost", user="myusername", passwd="mypassword"), then mycursor = mydb.cursor() and mycursor.execute("CREATE DATABASE mydatabase").

↑ ↓ move↵ openTab switch type