UPDATE语句错误 - MySQLdb / Python

时间:2013-06-17 01:46:46

标签: python mysql mysql-python

我正在测试表中的现有记录上测试UPDATE语句。它看起来像这样:

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

当我运行此代码时,我收到以下错误:

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

我无法理解,因为该列确实存在该名称。你能帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

我解决了。代码应如下所示:

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