Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created September 8, 2015 18:24
Show Gist options
  • Save doobeh/b16e800cdd51d6413c09 to your computer and use it in GitHub Desktop.
Save doobeh/b16e800cdd51d6413c09 to your computer and use it in GitHub Desktop.
SQLAlchemy bulk updates.
""" Quick example showing how to do a bulk update into the database
using SQLAlchemy, without interacting heavily with the ORM.
"""
from sqlalchemy.sql.expression import bindparam
stmt = addresses.update().\
where(addresses.c.id == bindparam('_id')).\
values({
'user_id': bindparam('user_id'),
'email_address': bindparam('email_address'),
})
conn.execute(stmt, [
{'user_id': 1, 'email_address' : 'jack@yahoo.com', '_id':1},
{'user_id': 1, 'email_address' : 'jack@msn.com', '_id':2},
{'user_id': 2, 'email_address' : 'www@www.org', '_id':3},
{'user_id': 2, 'email_address' : 'wendy@aol.com', '_id':4},
])
@victorjabur
Copy link

what is addresses ?

@jha-hitesh
Copy link

@hilmanski
Copy link

Thanks! I made an article based on this example in case someone need help.
How to bulk update in sqlalchemy

@hilmanski
Copy link

Hi @doobeh are you familiar with bulk_update_mapping.
WDYT compare to this https://docs.sqlalchemy.org/en/14/_modules/examples/performance/bulk_updates.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment