我在使用 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 的新手,我已经尝试过查看发布的问题,谢谢。
最佳答案
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 , "参数可以作为序列或映射提供..."
关于Python mysql 删除行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20089763/