Python/MYSQL How do i insert multiple records into a table with unknown number of values?

Viewed 43

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 Answers

Use list comprehension

sql = "INSERT INTO table_name VALUES(" + ",".join(["%s" for i in list_of_records]) + ")"
Related