2013-04-12 71 views
1

我正在寫一個函數來從表中刪除一條記錄。我試圖在刪除語句中引用的變量是一個Tkinter輸入字段。引用SQLite3中的一個python變量DELETE語句

我使用.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。我已經嘗試將其轉換爲一個字符串,並使用(?)作爲參數,但這也沒有工作。

回答

5

parameterized查詢正確的語法是:

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

我相信,你只需要調用commit的引用,而不是它

conn.commit()

+0

謝謝!畢竟這是一個愚蠢的錯誤 –