Reputation: 11
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.
Upvotes: 1
Views: 407
Reputation: 158
Use list comprehension
sql = "INSERT INTO table_name VALUES(" + ",".join(["%s" for i in list_of_records]) + ")"
Upvotes: 1