首页 > 解决方案 > 给定指定的 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 结束

标签: pythonmysql

解决方案


尝试将参数设为元组而不是字符串:

    ...
    sql = "DELETE FROM Cases WHERE caseid = %s"
    adr = str(selecteditem[1])
    c.execute(sql, (adr,))  # pass the argument as a tuple

推荐阅读