Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Python meets MySQL
EXPERIMENT 79 / 101

Open a conversation with SQL.

Connect with Connector/Python and use the downloadable lab fixture deliberately.

ADVANCED LAB
BY NAVAN SAMI◷ 9 MIN READPYTHON 3

01 The idea

MySQL is a separate server; installing its Python connector does not install or start that server. The workbench provides a local Docker Compose service, environment configuration, and a mysql_lab context manager. That helper connects to the dedicated practice database, creates temporary fixture tables for this connection, and cleans up the connection at the end. Each lesson starts from the same four tasks and two projects, so examples can be run independently.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

mysql_connect.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    cursor.execute("SELECT COUNT(*) AS total FROM lab_tasks")
    row = cursor.fetchone()
    print(f"Connected: {row['total']} practice tasks")
↳ EXPECTED OUTPUT
Connected: 4 practice tasks

03 Under the hood

  1. 1

    Download and unpack the workbench, create .env from .env.example, set local practice passwords, install requirements, and start MySQL before running this module. The Workbench page provides the exact commands.

  2. 2

    A connection represents a server session; a cursor issues statements and retrieves results. The helper requests dictionary rows and buffered results to make these small examples easy to inspect.

  3. 3

    Only lab_projects and lab_tasks temporary tables are seeded. They disappear with the session, so this teaching fixture is not a persistent application database. Build a persistent schema deliberately when you move beyond the exercises.

04 Your turn, builder

A SMALL MISSION

Query the number of practice projects and print the result.

Reveal one possible solution +
one_possible_solution.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    cursor.execute("SELECT COUNT(*) AS total FROM lab_projects")
    print(cursor.fetchone()["total"])

05 Check your signal

QUICK SIGNAL CHECK

Does pip installing the connector start a MySQL server?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.