【问题标题】:Getting Error in cursor.execute while using mysql.connector in python在 python 中使用 mysql.connector 时出现 cursor.execute 错误
【发布时间】:2020-06-13 05:45:31
【问题描述】:

如何解决这个问题: 执行以下代码时出现 EOF 等错误----

我的代码是:

import mysql.connector as sql

db_con=sql.connect(host="localhost",user="root",passwd="XXXX",database="mydb")

db_cur=db_con.cursor()

db_cur.execute('"update Orders set Freight=case when ShipCountry='USA' then      Freight+(0.15*Freight)"' `" when ShipCountry='Canada' then Freight+(0.15*Freight)"``" when ShipCountry='France' then Freight+(0.10*Freight)"``" when ShipCountry='Germany' then Freight+(0.10*Freight)"``" when ShipCountry='Belgium' then Freight+(0.10*Freight)"``" else Freight+(0.05*Freight) end"`


how to solve?????

【问题讨论】:

  • 请扩展您的代码示例——至少到您错误的第 14 行。

标签: python mysql-python


【解决方案1】:

请关闭db_cur.execute() 的大括号并按照以下步骤操作。

  1. 提交查询

  2. 关闭光标

  3. 关闭连接。

【讨论】:

    【解决方案2】:

    您需要提交 INSERT 和 UPDATE 查询,并在完成后关闭连接。

    最好使用上下文管理器

    像这样:

    import mysql.connector as sql
    db_con=sql.connect(host="localhost",user="root",passwd="Redmilenovo#T",database="datagrokr")
    with db_con.cursor() as cursor:
        cursor.execute("""YOUR QUERY HERE""")
    
    db_con.commit()
    db_con.close()
    

    您也可以在上下文管理器中定义连接变量。

    【讨论】:

      【解决方案3】:

      EOF 代表文件结束。此错误通常意味着在某行某处有一个左括号,但没有匹配的右括号。

      db_cur.execute('"update Orders set Freight=case when ShipCountry='USA' then      Freight+(0.15*Freight)"' `" when ShipCountry='Canada' then Freight+(0.15*Freight)"``" when ShipCountry='France' then Freight+(0.10*Freight)"``" when ShipCountry='Germany' then Freight+(0.10*Freight)"``" when ShipCountry='Belgium' then Freight+(0.10*Freight)"``" else Freight+(0.05*Freight) end"`
      

      如果你观察到他们没有右括号

      【讨论】:

        猜你喜欢
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 2015-08-16
        • 2020-12-06
        • 2020-06-28
        相关资源
        最近更新 更多