没有WHERE子句,python mysql UPDATE不起作用

时间:2013-12-31 07:55:38

标签: python mysql

    def WriteToDB():
import mysql.connector
var1 = comboboxRow1x1.get(), comboboxRow1x2.get()
var2="happy"
cnx = mysql.connector.connect(user='me', password='mine',
                            host='localhost',
                            database='testdb')
cursor = cnx.cursor()

cursor.execute ("""
        UPDATE curweek
        SET row1=%s
        WHERE row3=%s;
        """, (str(var1), var2))
cnx.commit()
cnx.close()

以上工作,但当我删除WHERE时,它会出错;

第91行,在WriteToDB中     “”,“(str(var1)))   执行文件“/usr/lib/python2.7/site-packages/mysql/connector/cursor.py”,第381行     “字符串格式化过程中的参数数量错误”) ProgrammingError:字符串格式化过程中的参数数量错误

这是我尝试删除WHERE的方法之一;

    def WriteToDB():
import mysql.connector
var1 = comboboxRow1x1.get(), comboboxRow1x2.get(), comboboxRow1x3.get()
#var2="happy"
cnx = mysql.connector.connect(user='me', password='mine',
                            host='localhost',
                            database='testdb')
cursor = cnx.cursor()

cursor.execute ("""
        UPDATE curweek
        SET row1=%s;
        """, (str(var1)))
cnx.commit()
cnx.close()

非常感谢你的帮助。格式化在这里 - 我还没有发布足够的内容以使其完美但在我的文件中它应该是我想的。

1 个答案:

答案 0 :(得分:2)

你需要传递一个元组(注意额外的逗号):

cursor.execute("UPDATE curweek SET row1=%s;", (str(var1),))

如果省略逗号,则传递字符串:

>>> i = 'hello'
>>> z = (i)
>> z
'hello'
>>> type(z)
<type 'str'>
>>> z = (i,)
>>> type(z)
<type 'tuple'>
>>> z
('hello',)
相关问题