Saqib Malik
Saqib Malik

Reputation: 98

Insert variable in sqlite value

The code i wrote is:

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)

I want to use the input values of ID,Name,Qty and Price to insert the values in the database but it gives me this error:

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

Upvotes: 1

Views: 1951

Answers (1)

Daniel
Daniel

Reputation: 42748

sql should be a string not a tuple, you already have your parameters in product:

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

Upvotes: 1

Related Questions