赞
踩
Update in 2023
SQLAlchemy has it’s own bulk operation. See doc on bulk operation for SQLAlchemy here.
Session.bulk_update_mappings() # For dictionaries
Session.bulk_save_objects # For objects
https://docs.sqlalchemy.org/en/14/orm/persistence_techniques.html#bulk-operations
from sqlalchemy import bindparam, update
# bindparam 字段必须和需要更新的表的字段不同,可以加 _ 来区分,如 material 为需要更新的字段,_material 为 bindparam 的字段
# values 里面的字段必须和需要更新的表的字段相同
stmt = update(Stock_rpa_mm373). \
where(Stock_rpa_mm373.material == bindparam('_material'),
Stock_rpa_mm373.location == bindparam('_location')). \
values({
'code': bindparam('code'),
})
session.execute(stmt, [
{'material': '0101HD400-000-G-BT', 'code': 'jack@yahoo', '_material': '0101HD400-000-G-BT', '_location': 'BA1C '},
])
session.commit()
参考
https://onebite.dev/how-to-bulk-update-in-sqlalchemy-python/
session.query(User).filter(User.district=='shandong').update({User.arrress: '中国'})
session.commit()
session.query(User).filter(User.district=='shandong').delete()
session.commit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。