1. 程式人生 > 其它 >pymysql同時執行多條語句時報錯

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在0.8版本以前(不包含0.8)是預設可以同時執行多條sql語句的,但是在0.8之後不再設定為預設,需要手動配置
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