1

As the title suggest, i'm struggling to find a way to insert multiple records into a table with an unknown amount of values?

The way that i was taught: sql = "INSERT INTO table_name VALUES(%s,%s,%s)" mycursor.execute(sql, list_of_records)

but obviously this assumes you know how many values that's going to be inserted. I'm wondering if there's another way to do this. I can't find any solution online.

1
  • Always list columns in INSERT INTO queries. Commented Mar 20, 2022 at 2:24

1 Answer 1

1

Use list comprehension

sql = "INSERT INTO table_name VALUES(" + ",".join(["%s" for i in list_of_records]) + ")"
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.