gpt4 book ai didi

python - 使用 sql alchemy 更新多个字段

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:47 24 4
gpt4 key购买 nike

    user = { "email" : "my_new_email@example.com", "age" : 20, "city" : "London", "country" : "UK", "language" : "English", "profession" : "developer", "employeer" : "BBC" }

admin = User.query.filter_by(username='admin').first()

admin.email = user["email"]
admin.age = user["age"]
admin.city = user["city"]
admin.country = user["country"]
admin.language = user["language"]
admin.profession = user["profession"]
admin.employeer = user["employeer"]
....
....
db.session.commit()

有什么办法可以避免这种隐式更新吗?我的意思是,如果只更新一两个字段,这种方法就很好用,但对于大量数据进行大规模更新则不可行。

最佳答案

您可以将该字典提供给更新子句:

data = { "email" : "my_new_email@example.com", "age" : 20, "city" : "London", "country" : "UK", "language" : "English", "profession" : "developer", "employeer" : "BBC" }
User.query.filter_by(username='admin').update(data)
db.session.commit()

关于python - 使用 sql alchemy 更新多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com