在python中使用mysql.connector时,cursor.execute中出现错误



如何解决此问题:我在执行以下代码时遇到了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?????

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

  1. 提交到查询

  2. 关闭光标

  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代表文件结束。这个错误通常意味着一行的某个地方有一个左括号,但没有匹配的右括号。
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"`

如果你观察到他们的无右括号

最新更新