[英]Can anyone help me with sqlite3.OperationalError: database is locked?
我目前正在使用 Python、SQLite3 和 Tkinter 編寫數據庫程序。 用戶在訂單上輸入唯一的 ProductID 和該產品的數量,程序從產品表中取出價格,並乘以用戶輸入的數量,得到訂單的總成本。 運行代碼后,我收到以下錯誤:
File "c:\Users\Ryan\OneDrive - C2k\A2 2020-2021\Computer Science\A2 Unit 5\Code\OrderForm.py",
line 80, in CalculatePrice
cursor.execute('INSERT INTO Orders(OrderTotal) VALUES (?)', (price,))
sqlite3.OperationalError: database is locked
我已經對此錯誤進行了研究,但是我完全不知道如何修復它。 有沒有人在知道該怎么做之前遇到過這個錯誤? 我附上了我的代碼片段和下面表單 GUI 的屏幕截圖。
def CalculatePrice(self):
orderid = self.OrderIDEntry.get()
productid = self.ProductIDEntry.get()
quantity = self.QuantityEntry.get()
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
cursor.execute('SELECT Price FROM Products WHERE ProductID = ?', (productid,))
result = cursor.fetchone()
if result:
price = int(quantity) * float(result[0])
cursor.execute('INSERT INTO Orders(OrderTotal) VALUES (?)', (price,))
self.ClearEntries()
else:
tkinter.messagebox.showerror("Error", "No product was found with this ProductID, please try again.")
self.ClearEntries()
cursor
的第二次使用是在with
塊之外,所以數據庫連接不再有效。
把它移到里面:
def CalculatePrice(self):
orderid = self.OrderIDEntry.get()
productid = self.ProductIDEntry.get()
quantity = self.QuantityEntry.get()
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
cursor.execute("SELECT Price FROM Products WHERE ProductID = ?", (productid,))
result = cursor.fetchone()
if result:
price = int(quantity) * float(result[0])
cursor.execute("INSERT INTO Orders(OrderTotal) VALUES (?)", (price,))
else:
tkinter.messagebox.showerror("Error", "No product was found with this ProductID, please try again.")
self.ClearEntries()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.