I have a list of lists, e.g [['a','b'],['c','d']]
.
I have a table called T
and two fields F1
, F2
. The first item in the field list maps to F1
, second to F2
.
How can I insert rows for each inner list in a single command or call, rather than using a for loop like this?
for i in [['a','b'],['c','d']]: c.execute("insert into T (F1,F2) values (%s, %s)", (i[0], i[1]))
INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.
If you want to insert more rows than that, you should consider using multiple INSERT statements, BULK INSERT or a derived table. Note that this INSERT multiple rows syntax is only supported in SQL Server 2008 or later. To insert multiple rows returned from a SELECT statement, you use the INSERT INTO SELECT statement.
From MySQLdb User's Guide:
c.executemany( """INSERT INTO breakfast (name, spam, eggs, sausage, price) VALUES (%s, %s, %s, %s, %s)""", [ ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ), ("Not So Much Spam Plate", 3, 2, 0, 3.95 ), ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 ) ] )
so in your case:
c.executemany("insert into T (F1,F2) values (%s, %s)", [('a','b'),('c','d')])
It's possible to insert all rows in one single statement like @adamhajari, and avoid sql injections like @zenpoy, at the same time. You just need to create a big insert statement and let mysqldb's execute
do the formatting.
values_to_insert = [('a','b'),('c','d')] query = "INSERT INTO T (F1, F2) VALUES " + ",".join("(%s, %s)" for _ in values_to_insert) flattened_values = [item for sublist in values_to_insert for item in sublist] c.execute(query, flattened_values)
Not super readable, but can be slightly faster than executemany (I tried inserting batches of 50000 rows in a local DB, executemany was 20% slower).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With