I want to insert a list like that ,but much larger.
list=[{"name":"Tom","gender":"male",},{"name":"Jack","gender":"male",},{"name":"Lee","gender":"male",}]
into MySQL using python,to build a table. I have already import MySQLdb .What should I do then?
Assuming you have a table named foo
with name
and gender
columns, here is how you can use executemany()
to insert this list of dictionary into the table:
import MySQLdb
db = MySQLdb.connect(...)
cursor = db.cursor()
data = [
{"name":"Tom", "gender":"male"},
{"name":"Jack", "gender":"male"},
{"name":"Lee", "gender":"male"}
]
cursor.executemany("""
INSERT INTO foo (name, gender)
VALUES (%(name)s, %(gender)s)""", data)
db.commit()
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