Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mysql行删除问题_Python_Mysql - Fatal编程技术网

Python mysql行删除问题

Python mysql行删除问题,python,mysql,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

我在使用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()

“参数可以作为序列或映射提供…”

您没有用任何值替换“%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()

如果将
print(repr(sid))
放在
sid=SIDEnt.get()
行之后,会打印什么?除了检查
sid的值之外,我认为您需要使用
作为占位符,而不是
%s
。如果他使用MySQLdb,
%s
是正确的参数样式。下面是解释这一点的方法:“为什么是元组?因为DB API要求您以序列形式传递任何参数。”感谢您添加了这一点,Burhan。事实上,单元素列表在这里也可以。令人惊讶的是,我从来没有想过在sid之后添加一个额外的逗号,非常感谢。@IfollyIrocked并将其放在一个框中?您肯定不想在SQL中使用“%”,因为它可能允许“SQL注入”。即使在这种特殊情况下,攻击者无法访问变量“sid”,使用它也被视为不好的做法。