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
这消除错误,但没有做任何事情 –
该数据库的代码是http://www.heypasteit.com/clip/38QJ –
@SaqibMalik你是什么意思,它不会做任何事情?你是否在用户输入后检查数据库,并通知用户的条目添加到数据库?为我工作.. – davedwards