0

I have a list which I get from the form:

hobby = request.form.getlist('hobby')

The list is something like:

hobby = [sports, music, coding]

I would like to store this list in mysql server, so I have tried:

cursor.executemany('INSERT into hobby(list,a_id) VALUES(%s,%s)', (hobby, current_user.id))
return '<h1> Inserted </h1>'
5
  • 1
    Very nice. What is not working? Commented Dec 25, 2017 at 18:39
  • 2
    Would it kill you to write a title that has more than 3 words and explains your problem concisely? (It's mentioned in the tour you seem to have gone through) Commented Dec 25, 2017 at 18:42
  • its showing TypeError: not enough arguments for format string Commented Dec 25, 2017 at 18:42
  • anoy.nmous ... please add the error message into the body of your question - NOT inside comments. Commented Dec 25, 2017 at 18:52
  • Do not call variables list, it overrides the list class reference. Commented Dec 25, 2017 at 19:01

1 Answer 1

1

The cursor.executemany takes as second parameter an iterable of iterables. Every item will map to one time you execute the query. Every item then contains an iterable that contains the parameters to fill in.

So for this case, we should construct it like:

cursor.executemany('INSERT into hobby(list,a_id) VALUES(%s,%s)',
                   [(interest, current_user.id) for interest in interests])

N.B.: do not call variables things like list, since you will override the reference to the builtin list class. Here we named the list interests.

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.