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()
4
  • 2
    The executemany method may also be of interest.
    – DanielB
    May 24, 2020 at 6:09
  • 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()
    – azulu
    May 24, 2020 at 6:43
  • And obviously this just adds the same values from cartproducts into the Product_Price column
    – azulu
    May 24, 2020 at 6:44
  • What about: for item, price in zip(cartproducts, cartprices): c.execute("INSERT INTO orders(Product_Name, Product_Price) VALUES(?,?);", (item, price, )) connection.commit()
    – ruohola
    May 24, 2020 at 8:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.