0

Hi I'm trying to implement a submit order function for a homework assignment in python

def order_submitted():
for item in cartproducts:
    c.execute("INSERT INTO orders(Product_Name) VALUES(?);", str((cartproducts,)))
c.commit()

This is the code I currently have and I have no idea why I am getting the Incorrect number of bindings error, I've looked at the other posts about this error, and tried the fixes to no avail, just wondering if anyone can see whats wrong with this code. Also I've tried the code without the str() but then I just run into another data type error.

0

1 Answer 1

2

You are supplying the whole list converted to a string as the parameter values for the query. It should instead be something like this:

def order_submitted():
    for item in cartproducts:
        c.execute("INSERT INTO orders(Product_Name) VALUES(?);", (item, ))
    c.commit()
Sign up to request clarification or add additional context in comments.

4 Comments

The executemany method may also be of interest.
If I could just ask you something else, now for my code I have to add in the prices for these items as well from a different list which is cartprices, how would I go about this? So far I've got for item in cartproducts: c.execute("INSERT INTO orders(Product_Name, Product_Price) VALUES(?,?);", (item, item, )) connection.commit()
And obviously this just adds the same values from cartproducts into the Product_Price column
What about: for item, price in zip(cartproducts, cartprices): c.execute("INSERT INTO orders(Product_Name, Product_Price) VALUES(?,?);", (item, price, )) connection.commit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.