python - 给定指定的 nameid 或 productname,代码无法删除 mysql 表中的行
问题描述
用于删除所选项目的代码:
def deletecase(self):
if not self.tree.selection():
ms.showerror('ERROR!!', 'INVALID SELECTION')
else:
result = ms.askquestion('CONFIRMATION!!', 'Are you sure you want to delete this record%s', icon="warning")
if result == 'yes':
curItem = self.tree.focus()
contents = (self.tree.item(curItem))
selecteditem = (contents['values'])
print(selecteditem)
self.tree.delete(curItem)
print(selecteditem[1])
sql = "DELETE FROM Cases WHERE caseid = %s"
adr = str(selecteditem[1])
c.execute(sql, adr)
这是错误消息:
Exception in Tkinter callback
[32, 'Big Case', 0, 'water', 320, 350, 50, 0]
Big Case
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
进程以退出代码 0 结束
解决方案
尝试将参数设为元组而不是字符串:
...
sql = "DELETE FROM Cases WHERE caseid = %s"
adr = str(selecteditem[1])
c.execute(sql, (adr,)) # pass the argument as a tuple
推荐阅读
- bash - 如何使用密码在非交互式 bash 脚本中 ssh
- java - Outlook 不会预览由 ical4j 创建的 ics 文件
- excel - 如果有超过 360 行数据,VBA 代码会提前离开循环
- java - 如何在java中只打印时间?
- c# - 如果有人将字符串输入双精度并且在发送错误消息后,他们如何再次发送错误
- java - 为什么不能用 sysem.out.print 命令打印 2 个字符
- angular - Angular Azure AD - MsalModule。认证后获取Access Token
- javascript - Momentjs如何获取时区时间 - 不基于计算机日期时间
- html - 需要帮助了解 IE 中加载的 HTML 页面中的图像如何存储/访问(Excel VBA)
- javascript - 如何使用 Javascript 基于另一个 div 设置 div 的样式?