简体   繁体   中英

INSERT statement error - MySQLdb/Python

I have the following code:

cur.execute("INSERT INTO term_results(%s, %s) VALUES (%s, %s) WHERE results_id = %s", (term, termInserted, nResult, bResult, mostRecentRecord))

term and termInserted are both strings. The rest are integers. I get the following error:

Error 1064: You have an error in your SQL syntax;

I have tried rearranging the WHERE clause but no luck, can you help? Thanks.

You cannot use where with the values form of insert . Just use insert . . . select insert . . . select insert . . . select :

INSERT INTO term_results(%s, %s)
    SELECT %s, %s
    from <somewhere>
    WHERE results_id = %s"

I don't know what the expression (term, termInserted, nResult, bResult, mostRecentRecord) is supposed to be doing. You also need a from name with a table that has a column called results_id .

If the list are actually more columns, then perhaps you want something like this:

INSERT INTO term_results(%s, %s, term, termInserted, nResult, bResult, mostRecentRecord)
    SELECT %s, %s, term, termInserted, nResult, bResult, mostRecentRecord
    from <somewhere>
    WHERE results_id = %s"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM