UPDATE statement error - MySQLdb/Python

二次信任 提交于 2019-12-24 17:19:17

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/17139423/update-statement-error-mysqldb-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!