文章

Python 首批细教程 · 03B:用 PyMySQL 做一遍真正的 CRUD

#270 · 2026-05-13 · Python 教程拆解

对应原仓库Day36-40/39.Python程序接入MySQL数据库.md

已提供示例脚本/tutorial-assets/python-100-days/03b-python-mysql-crud/(站点源码路径:blog-src/static/tutorial-assets/python-100-days/03b-python-mysql-crud/

原仓库这一段最值钱的地方,是把“Python 接库”拆成了真正的五步:连接、游标、执行 SQL、提交事务、关闭连接。

这篇我们按那个步骤走一遍。

Step 1:安装依赖

pip install pymysql cryptography

如果你连的是 MySQL 8,这两个基本是常见组合。

Step 2:准备一张测试表

先在 MySQL 里执行:

create database if not exists demo_db default character set utf8mb4;
use demo_db;

create table if not exists tb_dept (
  dno int primary key,
  dname varchar(20) not null,
  dloc varchar(20) not null
);

Step 3:插入一条数据

示例目录已经提供 insert_dept.py(站点源码路径:blog-src/static/tutorial-assets/python-100-days/03b-python-mysql-crud/insert_dept.py):

import pymysql

conn = pymysql.connect(
    host="127.0.0.1",
    port=3306,
    user="guest",
    password="Guest.618",
    database="demo_db",
    charset="utf8mb4"
)

try:
    with conn.cursor() as cursor:
        affected_rows = cursor.execute(
            "insert into tb_dept values (%s, %s, %s)",
            (10, "研发部", "成都")
        )
        print("影响行数:", affected_rows)
    conn.commit()
except pymysql.MySQLError as err:
    conn.rollback()
    print("失败:", err)
finally:
    conn.close()

这里最重要的不是记代码,而是记流程:

  1. 先连库;
  2. 再拿游标;
  3. 再发 SQL;
  4. 成功就 commit()
  5. 出错就 rollback()
  6. 最后 close()

Step 4:查询数据

示例目录已经提供 select_dept.py(站点源码路径:blog-src/static/tutorial-assets/python-100-days/03b-python-mysql-crud/select_dept.py):

import pymysql

conn = pymysql.connect(
    host="127.0.0.1",
    port=3306,
    user="guest",
    password="Guest.618",
    database="demo_db",
    charset="utf8mb4"
)

try:
    with conn.cursor() as cursor:
        cursor.execute("select dno, dname, dloc from tb_dept")
        row = cursor.fetchone()
        while row:
            print(row)
            row = cursor.fetchone()
finally:
    conn.close()

这也是原仓库里强调的点:不要一上来就 fetchall()。逐行读取在大结果集里更稳。

Step 5:自己补全 Update / Delete

你应该顺着这两种 SQL 自己练:

update tb_dept set dname=%s, dloc=%s where dno=%s
delete from tb_dept where dno=%s

常见坑

  • root 账号直连业务库。
  • 修改数据后忘记 commit()
  • 所有查询都 fetchall(),结果集一大就吃内存。