2017-01-12 96 views
-2

有誰知道如何在python sqlite的執行裏面的值的查詢使用SQLite中值的查詢 - 蟒蛇

我收到eroor是: 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

編輯問題,格式代碼。 – furas

+0

@furas你是什麼意思 –

+0

'結果'可以是迭代器'。您必須從interator獲取單個行,然後從行獲取單個元素。 – furas

回答

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

我通過添加result = str(result)來處理它,但是它將值插入到數據庫中,像這樣[(7,)]你是否知道如何使它插入7 –

+0

參考這個:http:// stackoverflow.com/questions/12867140/python-mysqldb-get-the-result-of-fetchall-in-a-list – Bambang

+0

謝謝無論如何通過剝離字符串工作正常 –