bulk insert list values with SQLAlchemy Core
Here's one way to do it:
MyTable.__table__.insert().execute([{'color': 'blue'},
{'color': 'red'},
{'color': 'green'}])
Or, using connection.execute()
:
conn.execute(MyTable.insert(), [{'color': 'blue'},
{'color': 'red'},
{'color': 'green'}])
You can easily make a list of dicts from the list you have:
[{'color': value} for value in colors]
Another way to do it:
from sqlalchemy import MetaData, Table, create_engine
engine = create_engine("mysql+mysqlconnector://....")
metadata = MetaData()
metadata.reflect(engine, only=['MyTable'])
table = Table('MyTable', meta, autoload=True, autoload_with=engine)
engine.execute(table.insert(), [{'color': 'blue'},
{'color': 'red'},
{'color': 'green'}])