Python - MySQLdb - 更新行导致TypeError:格式字符串的参数不足

时间:2013-04-26 11:21:22

标签: python mysql

我尝试了很多方法,我似乎无法弄明白,但无论如何,这就是我正在做的事情:

threadupdatequery = "UPDATE %s SET topic_name=%%s, subject_name=%%s, poster_name=%%s, time=%%s, image=%%s, replies=%%s, id=%%s, keywords=%%s, text=%%s WHERE id=%%s" % ('Threads')
...
(topic, subject, name, time, image, replies, ID, kw, body) = self.tframe.Get()
cur.execute(threadupdatequery, (topic, subject, name, time, image, replies, ID, kw, body))

最初我这样做了,我毫无疑问会工作,但我错了:

cur.execute(threadupdatequery, self.tframe.Get())

我一直在:

File "C:\Python27\Work\SiteEditor.py", line 216, in UpdateThread
cur.execute(threadupdatequery, (topic, subject, name, time, image, replies, ID, kw, body))
File "C:\Python27-32Bit\lib\site-packages\MySQLdb\cursors.py", line 184, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string

我究竟做错了什么?通常我犯了一些愚蠢的错误,但这次我只是看不到......

谢谢:)

1 个答案:

答案 0 :(得分:1)

更新查询有10个%%s占位符,但execute()调用只有9个替换变量。最后WHERE id=%%s无法替换。如果添加缺失的变量,它可能会起作用。