在python中使用mysql.connector时在cursor.execute中获取错误

2024-09-27 17:56:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何解决这个问题: 我在执行下面的代码时遇到了类似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?????

Tags: 代码importdbsqlconnectoras错误mysql
3条回答

您需要提交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()

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

EOF stands for End Of File. This error usually means that there was an open parenthesis somewhere on a line, but not a matching closing parenthesis.

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"`

如果你观察到它们,就没有右括号

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

  1. 提交查询

  2. 关闭光标

  3. 关闭连接

相关问题 更多 >

    热门问题