Python mysql行删除问题

时间:2013-11-20 07:21:10

标签: python mysql

我在使用python从数据库中删除行时遇到问题。我可以像这样硬编码:

del_student = "DELETE FROM students WHERE sid=34556"
cursor.execute(del_student)
cnx.commit()

但我正试图从我的gui中的输入框中获取sid并且这不起作用:

sid=SIDEnt.get()
del_student = "DELETE FROM students WHERE sid=%s"
cursor.execute(del_student,sid)
cnx.commit()

我对python很陌生,我已经尝试查看已发布的问题了,谢谢。

2 个答案:

答案 0 :(得分:2)

sid需要在元组中。

sid=SIDEnt.get()
del_student = "DELETE FROM students WHERE sid=%s"
cursor.execute(del_student,(sid,)) # put `sid` into a one-element tuple
cnx.commit()

DB-API specifies,“参数可以作为序列或映射提供......”

答案 1 :(得分:0)

您没有用任何值替换'%s';所以你的查询是这样的:

'DELETE FROM students WHERE sid=%s'

应该如下:

sid=SIDEnt.get()
del_student = "DELETE FROM students WHERE sid=%s"%(sid,)
cursor.execute(del_student,sid)
cnx.commit()