在sqlite值中使用查询 - python

时间:2017-01-12 21:58:45

标签: python sqlite

有没有人知道如何在python sqlite

中的值内执行查询

我得到的是: sqlite3.InterfaceError:绑定参数1时出错 - 可能是不支持的类型。

我的代码在这里:

Name = input("Enter the name of the product you want to purchase: >>")
item = Name
qty = input("Enter the Quantity of the product you want to purchase: >>")

today = date.today()
cursor = db.cursor()
cursor.execute("SELECT CatID from Products where Name=?",(Name,))
result = cursor.fetchall()

 confirm = input("are you sure you want tot buy this product (y/n): >>" )

if confirm == "y":

     ### In this query where it says result i want to execute the data from the result query
cursor.execute("INSERT INTO OrderHistory(Username,Category,Date,Qty,ItemHistory) Values(?,?,?,?,?)",(Username,result,today,qty,Name))

db.commit()

print("Product purchased. Thankyou for your order")

cursor.execute("UPDATE Products SET Qty = (? -1) where Name = ?",(qty,item,))

else:

print("The program will now terminate")  

1 个答案:

答案 0 :(得分:0)

您还可以迭代结果:

for row in result:
    cursor.execute(
    "INSERT INTO OrderHistory(Username,Category,Date,Qty,ItemHistory) SELECT CatID,?,?,?,? FROM Products WHERE Name=?",(Username,row,today,qty,Name))
    db.commit()