作为一名长期从事Python开发的工程师,我经常需要处理各种数据存储和查询任务。这次实验将带大家完整走一遍Python与MySQL数据库交互的全流程,从环境搭建到实际应用,涵盖pymysql和pandas两大核心工具的使用。
这个实验特别适合以下人群:
实验环境要求:
提示:实验过程中请保持MySQL服务运行,不要关闭命令行窗口
很多同学在第一步安装MySQL时就容易遇到问题,这里分享我总结的可靠安装方法:
bash复制# 将mysql-8.0.30-winx64.zip解压到C:\ProgramData
# 确保路径为C:\ProgramData\mysql-8.0.30-winx64
bash复制cd C:\ProgramData\mysql-8.0.30-winx64\bin
.\mysqld --initialize --console # 注意记录输出的临时密码
.\mysqld -install
net start mysql
bash复制.\mysql -u root -p # 使用刚才的临时密码登录
alter user 'root'@'localhost' identified by '123456';
commit;
在实际教学中,我发现同学们常遇到这些问题:
.\mysqld --remove然后重新安装pymysql是Python连接MySQL最常用的库,下面是标准连接方式:
python复制import pymysql
# 建立数据库连接
db = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='student_101' # 可以省略,后续用USE语句选择
)
# 创建游标对象
cursor = db.cursor()
# 执行SQL语句
cursor.execute("CREATE DATABASE IF NOT EXISTS student_101")
cursor.execute("USE student_101")
# 关闭连接
cursor.close()
db.close()
注意:一定要记得关闭连接,否则会导致连接泄漏
让我们实现将Excel学生名单导入MySQL的功能:
python复制from openpyxl import load_workbook
def import_students(file_path):
# 读取Excel文件
wb = load_workbook(file_path)
sheet = wb.active
# 连接数据库
db = pymysql.connect(host='localhost', user='root', password='123456')
cursor = db.cursor()
# 创建数据库和表
cursor.execute("CREATE DATABASE IF NOT EXISTS student_101")
cursor.execute("USE student_101")
cursor.execute("""
CREATE TABLE IF NOT EXISTS mingdan (
sno VARCHAR(20) PRIMARY KEY,
sname VARCHAR(50) NOT NULL
)
""")
# 插入数据
for row in sheet.iter_rows(min_row=2, values_only=True):
sno, sname = str(row[0]), str(row[1])
try:
cursor.execute("INSERT INTO mingdan VALUES (%s, %s)", (sno, sname))
except pymysql.err.IntegrityError:
print(f"学号{sno}已存在,跳过")
db.commit()
print(f"成功导入{sheet.max_row-1}条学生记录")
将数据库中的数据查询出来并转为字典:
python复制def export_to_dict():
db = pymysql.connect(host='localhost', user='root',
password='123456', database='student_101')
cursor = db.cursor()
cursor.execute("SELECT sno, sname FROM mingdan")
results = cursor.fetchall()
student_dict = {sno: sname for sno, sname in results}
cursor.close()
db.close()
return student_dict
创建存储学生成绩的表结构:
python复制def init_score_table():
conn = pymysql.connect(host='localhost', user='root',
password='123456', database='student_101')
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS scores (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
chinese FLOAT,
math FLOAT,
english FLOAT,
computer FLOAT
)
""")
# 插入示例数据
sample_data = [
("张三", 88, 90, 98, 95),
("李四", 85, 92, 95, 98),
("王五", 89, 89, 90, 92),
("丁六", 82, 86, 89, 90)
]
cursor.executemany("""
INSERT INTO scores (name, chinese, math, english, computer)
VALUES (%s, %s, %s, %s, %s)
""", sample_data)
conn.commit()
conn.close()
Pandas与MySQL的完美结合:
python复制import pandas as pd
def analyze_scores():
conn = pymysql.connect(host='localhost', user='root',
password='123456', database='student_101')
# 读取数据到DataFrame
df = pd.read_sql("SELECT * FROM scores", conn)
# 计算统计指标
df['平均分'] = df[['chinese', 'math', 'english', 'computer']].mean(axis=1)
df['最高分'] = df[['chinese', 'math', 'english', 'computer']].max(axis=1)
# 添加各科最高分行
max_scores = pd.DataFrame(df[['chinese', 'math', 'english', 'computer']].max()).T
max_scores['name'] = '最高分'
df = pd.concat([df, max_scores])
# 保存回数据库
df.to_sql('score_analysis', conn, if_exists='replace', index=False)
conn.close()
return df
python复制with pymysql.connect(...) as conn:
with conn.cursor() as cursor:
cursor.execute(...)
python复制# 使用executemany提高批量插入效率
data = [(...), (...), ...]
cursor.executemany("INSERT ... VALUES (%s, %s)", data)
python复制try:
cursor.execute("...")
cursor.execute("...")
conn.commit()
except:
conn.rollback()
python复制conn = pymysql.connect(..., charset='utf8mb4')
python复制cursor.execute("CREATE INDEX idx_name ON scores(name)")
python复制# 使用LIMIT实现分页
cursor.execute("SELECT * FROM scores LIMIT 10 OFFSET 20")
python复制from DBUtils.PooledDB import PooledDB
pool = PooledDB(pymysql, 5, host='...', user='...', ...)
这个基础框架可以进一步扩展为:
我在实际项目中总结的经验是,数据库操作的核心在于: