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