sqlAlchemy中的对象转换为dict

https://blog.csdn.net/jacke121/article/details/89292743

p = session.query(Post).first()
p.__dict__

#方法二
 arr = db.mappings().all()   
 arr2=[]
 for x in arr:
     x2 = dict(x)
     x2["childshow"] = "1" 
     arr2.append(x2)
     # print(x.keys())
     # print(x.items())

批量增加

users = [
    User(username='john', password='doe'),
    User(username='jane', password='smith'),
    User(username='bob', password='ross')
]
session.add_all(users)
session.commit()

#方法一bulk_save_objects
s = Session()
objects = [
    User(name="u1"),
    User(name="u2"),
    User(name="u3")
]
s.bulk_save_objects(objects)
s.commit()

#方法二bulk_insert_mappings
s.bulk_insert_mappings(User,
  [dict(name="u1"), dict(name="u2"), dict(name="u3")]
)
s.commit()
#两种方法效果一样

批量更新

session.query(User).filter(User.district=='shandong').update({User.arrress: '中国'})
session.commit()

批量删除

session.query(User).filter(User.district=='shandong').delete()
session.commit()