• 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

  • 相关阅读:
    2.SpringMVC入门
    1.SpringMVC回顾MVC
    10.spring声明式事务
    9.spring整合Mybatis
    maven项目的classpath路径对应的文件目录
    8.spring中的AOP
    Java通过反射破解单例模式和防止反射破解单例模式案例
    7.代理模式
    让Java项目默认使用jdk8等级进行编译
    return false取消手机移动端的默认设置
  • 原文地址:https://www.cnblogs.com/JackShi/p/14973451.html
Copyright © 2020-2023  润新知