1

I'm trying to pass form data in Flask into multiple rows of a sqlite database.

The form requires the user to enter multiple ingredients and ingredient amounts required, and I'm trying to pass these into individual rows, using the recipe name to group the rows together.

if request.method == "POST":

    rname = request.form.get('rname')
    customer_id = request.form.get('customer_id')
    ingredient_id = request.form.getlist('product_code')
    ingredient_amount = request.form.getlist('ingredient_amount')

    con = sqlite.connect('database.db')
    cur = con.cursor()
    sql = """INSERT INTO recipes (rname, customer_id, ingredient_id, ingredient_amount) VALUES (?, ?, ?, ?)"""
    cur.executemany(sql, (rname, customer_id, ingredient_id, ingredient_amount,))
    cur.commit()

Error

ValueError: parameters are of unsupported type

On the front-end, the data is being passed OK as I can see it within the network console.

Where is this code going wrong?

Thanks.

2
  • do you have any sort of data validation? perhaps the data being passed through does not match the data types specified in the database Commented Oct 15, 2019 at 21:53
  • Possible duplicate of How can I use executemany to insert into MySQL a list of dictionaries in Python. It looks like you need to create a compound list with consistent number of values per row. In other words, you need to duplicate the "single values" and interleave the lists so that they form one list per row. Right now you are trying to submit some single values along with separate lists, but that won't work. Also lookup web docs for executemany. Commented Oct 16, 2019 at 2:06

1 Answer 1

1

You should pass to Cursor.executemany a sequence of parameters (e.g a list of tuples, see documentation). Given error in your case means rname is not iterable (I am assuming it is a string).

You need to properly pack your data for executemany like so

query_args = []

for ind, ingredient in enumerate(ingredient_id):
    data = (rname, customer_id, ingredient, ingredient_amount[ind])
    query_args.append(data)

cur.executemany(sql, query_args)
Sign up to request clarification or add additional context in comments.

Comments

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.