lotexteam
lotexteam

Reputation: 11

Getting error from mysql request in python

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.

Upvotes: 1

Views: 34

Answers (1)

Mureinik
Mureinik

Reputation: 311326

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

Upvotes: 2

Related Questions