简体   繁体   中英

UPDATE statement error - MySQLdb/Python

I'm testing an UPDATE statement on an existing record in my test table. It looks like this:

term = 'example-column'
termInserted = term + '_inserted'
mostRecentRecord = 6
nResult = 777
bResultsInserted = 77

#print(termInserted)
cur.execute("UPDATE `term_results` SET `%s` = %s, `%s` = %s WHERE `results_id` = %s", (term, nResult, termInserted, bResultsInserted, mostRecentRecord))
connectToDb.commit()

When I run this code I get the following error:

Error 1054: Unknown column ''example-column'' in 'field list'

Which I can't understand because the column does exist by that name. Can you help? Thanks.

I solved it. The code should look like:

cur.execute("""UPDATE `term_results` SET `%s` = %s, `%s` = %s WHERE `results_id` = %s""" % (term, nResult, termInserted, bResultsInserted, mostRecentRecord))

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