2016-12-21 4 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