user8967185
user8967185

Reputation: 33

Getting Error in cursor.execute while using mysql.connector in python

How to solve this problem: I am getting error like EOF etc while executing the below code ----

My code is:

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?????

Upvotes: 1

Views: 2387

Answers (3)

Welcome_back
Welcome_back

Reputation: 1455

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

if you observe their no close parenthesis

Upvotes: 0

Vishnu Kiran
Vishnu Kiran

Reputation: 652

You need to commit INSERT and UPDATE queries and also close the connection after you are done.

It is best to use context managers

Like so:

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()

You can also define the connection variable in a context manager as well.

Upvotes: 1

Aman Raheja
Aman Raheja

Reputation: 615

Please close the brace for db_cur.execute() and follow the steps below.

  1. Commit to the query

  2. Close the cursor

  3. Close the connection.

Upvotes: 1

Related Questions