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,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/

相关文章:

mysql - 使用 MySQL 数据库的 VB.NET 登录

PHP和Mysql,关于价格字段中字段出现2次$、美元符号时删除记录

python - 参数比例及数学表达式

python - 如何在 json 格式的字符串中使用 str.format?

python - Pycharm多模块运行服务器

PHP 脚本不向 mysql 表中添加列

php - 登录时从数据库中调用带盐的河豚加密

python - Cython:如何将 C 函数分配给 Python/Cython 变量?

python - 如何将非 ASCII 字符编码的文件重命名为 ASCII

mysql - 如果 id 更改,SQL 重置变量值