pymysql同时执行多条语句时报错

1. 我的脚本

复制代码
import pymysql


sql = """
update BaseInfo_dailyinfo_temp set open=26.88,close=32.86,high=32.86,low=26.88,exchange_rate=21.83,data_date='2021-07-05',exchange_amount=715182688.0,
exchange_quantity=231947,price_change_amount=5.48,price_change_rate=20.01,update_time='2021-07-05 15:37:34' where code_id=1234;
update BaseInfo_dailyinfo_temp set open=19.11,close=21.6,high=21.6,low=18.51,exchange_rate=26.22,data_date='2021-07-05',exchange_amount=1309556816.0,
exchange_quantity=655858,price_change_amount=3.6,price_change_rate=20.0,update_time='2021-07-05 15:37:34' where code_id=352;
""" conn = pymysql.Connect(user='root', password='123', host='127.0.0.1', db='stockAnalysis', port=3306) cur = conn.cursor() cur.execute(sql) conn.commit()
复制代码

2. 报错截图

1. 

 

 

 

3. 原因及解决方法

1. pymysql在8.0版本以前(不包含8.0)是默认可以同时执行多条sql语句的,但是在8.0之后不再设置为默认,需要手动配置
2. 配置方法是在获取数据连接时,配置参数
client_flag=CLIENT.MULTI_STATEMENTS

4. 更改后的代码

复制代码
import pymysql
-- 导入模块
from pymysql.constants import CLIENT sql = """ update BaseInfo_dailyinfo_temp set open=26.88,close=32.86,high=32.86,low=26.88,exchange_rate=21.83,data_date='2021-07-05',exchange_amount=715182688.0, exchange_quantity=231947,price_change_amount=5.48,price_change_rate=20.01,update_time='2021-07-05 15:37:34' where code_id=1234; update BaseInfo_dailyinfo_temp set open=19.11,close=21.6,high=21.6,low=18.51,exchange_rate=26.22,data_date='2021-07-05',exchange_amount=1309556816.0, exchange_quantity=655858,price_change_amount=3.6,price_change_rate=20.0,update_time='2021-07-05 15:37:34' where code_id=352;""" conn = pymysql.Connect(user='root', password='123', host='127.0.0.1', db='stockAnalysis', port=3306, client_flag=CLIENT.MULTI_STATEMENTS) # 添加client_flag参数
cur = conn.cursor() cur.execute(sql) conn.commit()
复制代码

 

 参考地址:https://stackoverflow.com/questions/58544640/pymysql-unable-to-execute-multiple-queries

 

posted @   10132714  阅读(2159)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示