Enymu xfb
Enymu xfb

Reputation: 11

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

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

Answers (1)

Evyn
Evyn

Reputation: 158

Use list comprehension

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

Upvotes: 1

Related Questions