1

Trying to insert data to the table with python sending request

def insert_copart(name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present):
query = (
 "INSERT INTO copart " "(name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)"
 "VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
args = (name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)
try:
 db_config = read_db_config()
 conn = MySQLConnection(**db_config)
 cursor = conn.cursor()
 cursor.execute(query, args)
 if cursor.lastrowid:
 print('last insert id', cursor.lastrowid)
 conn.commit()
except Error as error:
 print(error)
finally:
 cursor.close()
 conn.close()

But getting error

1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, ke' at line 1`

I checked everything, but no results.

Mureinik
316k54 gold badges404 silver badges406 bronze badges
asked Jun 19, 2021 at 21:22

1 Answer 1

2

condition is a reserved word in MySQL. I recommend you rename that column and give it a name that isn't a reserved word. If that is not an option, you can escape it by using backticks:

query = (
 "INSERT INTO copart " 
 "(name, lot, vin, odometer, `condition`, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)"
 "VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
answered Jun 19, 2021 at 21:26
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! "condition" was a trouble, removing it all works fine!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.