"""
save image.py
二进制文件存储演示
"""
import pymysql
"""
写操作实例
"""
#链接数据库
db = pymysql.connect(host='localhost',
port=3306,
user='root',
password='123456',
database='stu',
charset='utf8')
#获取游标(操作数据库,执行sql语句)
cur = db.cursor()
#存储图片
with open('01.png','rb') as f:
data = f.read()
try:
sql = f"update class set image=%s \
where name='Abby'; "
cur.execute(sql,[data])#执行正确cur获取结果
db.commit()
except Exception as e:
db.rollback()#退回到commit执行之前的数据库状态
print(e)
#关闭数据库
cur.close()
db.close()
05-28