装驱动与连库

Python 操作 MySQL 主流有两个驱动:官方的 mysql-connector 和社区更轻量的 PyMySQL。新手选 PyMySQL,纯 Python 实现、装得快、接口直白。装好就能 connect。

装驱动与连库要点

装驱动与连库示例

						import pymysql

						db = pymysql.connect(
						    host="127.0.0.1",
						    user="app_user",
						    password="app_pass",
						    database="shop_db",
						    charset="utf8mb4"
						)
						print("连上了")
						db.close()
避坑提醒:连本地报 2003,先确认 MySQL 在跑、端口是 3306;报字符集错就加 charset='utf8mb4',别用老 utf8。
装驱动与连库 · Python 连接 知识卡片
卡片 02 / 06 · Python 连接(1080×1440 速查卡片)

Python 连接知识点