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