Reputation: 33
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
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
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
Reputation: 615
Please close the brace for db_cur.execute()
and follow the steps below.
Commit to the query
Close the cursor
Close the connection.
Upvotes: 1