2016-12-21 124 views
1

我寫的代碼是:插入變量

import sqlite3 
def insert_product(product): 
    with sqlite3.connect("main.db") as db: 
     cursor = db.cursor() 
     sql=("insert into Products(CatID,Name,Qty,Price)values(?, ?, ?, ?)", (ID,Name,Qty,Price)) 
     cursor.execute(sql,product) 
     db.commit() 

if __name__ == "__main__": 
    ID= int(input("enter the CatID of the product:>> ")) 
    Name = input("Enter the name of the Product you want to inssert: >>") 
    Qty = int(input("Enter the quantity of the stock available: >>")) 
    Price = int(input("Enter the price of the product: >>")) 
    product = (ID,Name,Qty,Price) 
    insert_product(product) 

我想用ID的輸入值,名稱,數量及價格,以插入數據庫中的值,但它給了我這個錯誤:

File "C:\Users\Admin\OneDrive\sql\insert_product.py", line 6, in insert_product cursor.execute(sql,product) ValueError: operation parameter must be str

回答

1

sql應該是一個字符串不是一個元組,你已經有參數product

sql = "insert into Products (CatID,Name,Qty,Price) values (?, ?, ?, ?)" 
    cursor.execute(sql, product) 
+0

這消除錯誤,但沒有做任何事情 –

+0

該數據庫的代碼是http://www.heypasteit.com/clip/38QJ –

+1

@SaqibMalik你是什麼意思,它不會做任何事情?你是否在用戶輸入後檢查數據庫,並通知用戶的條目添加到數據庫?爲我工作.. – davedwards