1

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?????
1
  • Please extend the sample of your code — at least to your erroneous line 14. Commented Feb 29, 2020 at 9:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I was not getting any error, but I forgot to commit & close :')
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.