我正在编写一个从表中删除记录的函数。我试图在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。我已经尝试将它转换为字符串并使用(?)作为参数,但这也不起作用。
答案 0 :(得分:6)
parameterized查询的正确语法是:
c.execute("DELETE FROM orders WHERE orders_id=?)", (orderToDelete,))
我相信你只需要调用commit而不是引用它
conn.commit()