【发布时间】:2021-03-10 12:05:39
【问题描述】:
我目前正在从事一个学校项目,我必须使用 Python、Tkinter 和 SQLite3 开发具有图形用户界面的数据库应用程序。用户在表单中输入 ProductID 和数量,我试图让程序使用 ProductID 从产品表中选择产品的价格,并将此价格乘以用户输入的数量,得到总成本并将此结果插入到订单表的总计字段中。
我收到以下错误:
File "c:\Users\Ryan\OneDrive - C2k\A2 2020-2021\Computer Science\A2 Unit 5\Code\OrderForm.py", line 81, in CalculatePrice
price = ((int(quantity)*(int(x) for x in results)))
TypeError: unsupported operand type(s) for *: 'int' and 'generator'
我已经对此错误进行了研究,但是,我仍然无法理解如何使其工作,甚至是否可能。如果有人可以查看我下面的代码并尝试帮助我完成此操作,我将不胜感激。
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()
searchprice = ('''SELECT Price FROM Products WHERE ProductID = ?''')
cursor.execute(searchprice, [(productid)])
results = cursor.fetchall()
if results:
for i in results:
price = ((int(quantity)*(int(x) for x in results)))
addprice = ('''INSERT INTO Orders(OrderTotal)
VALUES (?)''')
cursor.execute(addprice, [(price)])
self.ClearEntries()
else:
tkinter.messagebox.showerror("Error", "No product was found with this ProductID, please try again.")
self.ClearEntries()
【问题讨论】:
-
假设
ProductID是唯一的,searchprice将返回一个价格...您在results上循环多次。price = int(searchprice[0][0]) * int(quantity)应该足够了。 -
你应该细分任务。首先尝试使用具有给定产品 ID 的产品价格设置一个变量。 然后对该产品进行计算,然后将结果插入到订单表中。检查成功 e. G。每个步骤都有“打印”。
-
@MauriceMeyer 是的 ProductID 是唯一的。我明白你的解释,谢谢!我现在就试试这个。
-
@MichaelButscher 我明白了,谢谢你的解释。