1

我正在编写一个函数来从表中删除记录。我试图在 delete 语句中引用的变量是 Tkinter 输入字段。

我使用 .get() 方法获取变量,但是我不能在不返回错误的情况下将其传递给 SQLite 语句。

以下代码是框架的一部分,我只是在问题中添加了相关代码

from tkinter import *

import sqlite3

class EditOrdersForm(Frame):

        def __init__(self):

        Frame.__init__(self)
        self.master.title("Edit Orders form:")
        self.pack()

        def displayDelOrderOptions(self):
            self.deleteOrderOptionsLabel = Label(self, text="Enter the Order ID of the order you wish to delete: ").grid(row=numOrders+4, pady=2, sticky=W)
            self.deleteOrderOptionsEntry = Entry(self, bd=3, width=10)
            self.deleteOrderOptionsEntry.grid(row=numOrders+4, pady=5, column=1)
            global orderToDelete
            orderToDelete = self.deleteOrderOptionsEntry.get()
            self.deleteButton = Button(self, text = "Delete this order", command = self.deleteOrder)
            self.deleteButton.grid(row=numOrders+5, pady=5, column=1)
        def deleteOrder(self):
            conn = sqlite3.connect("testdb.db")
            c = conn.cursor()

            c.execute("DELETE FROM orders WHERE orders_id=:1)", (orderToDelete,))

            conn.commit
            c.close()

root = EditOrdersForm()
root.mainloop()

我遇到的问题是 WHERE 语句,我如何引用 orderToDelete。我尝试将其转换为字符串并使用 (?) 作为参数,但这也不起作用。

4

1 回答 1

6

参数化查询的正确语法是:

c.execute("DELETE FROM orders WHERE orders_id=?)", (orderToDelete,))

我相信你只需要调用 commit 而不是引用它

conn.commit()

于 2013-04-12T13:24:56.767 回答