Referring to a python variable in SQLite3 DELETE s

2019-05-30 18:32发布

I am writing a function to delete a record from a table. The variable I am trying to refer to in the delete statement is a Tkinter entry field.

I get the variable using the .get() method, but I can't then pass this into the SQLite statment without returning an error.

The following code is part of the frame, I've only added the relevant code to the problem

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()

The problem I have is with the WHERE statement, how do I refer to orderToDelete. I have tried converting it to a string and using (?) as the parameter but this didn't work either.

1条回答
可以哭但决不认输i
2楼-- · 2019-05-30 18:59

THe correct syntax for parameterized query is:

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

I believe you just need to call commit instead of reference it

conn.commit()

查看更多
登录 后发表回答