在Sqlalchemy中更新多列 [英] Updating multiple columns in Sqlalchemy

查看:975
本文介绍了在Sqlalchemy中更新多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在flask上运行的应用程序,该应用程序使用sqlalchemy与数据库进行交互.我想用用户指定的值更新表的列.我正在使用的查询是

I have an app that runs on flask and uses sqlalchemy to interact with the data base. I want to update the columns of a table with the user specified values. The query that I am using is

def update_table(value1, value2, value3):
     query = update(Table).where(Table.column1 == value1).values(Table.column2 = value2, Table.column3 = value3)

我不确定我传递值的方式是否正确.还有Table.column2/3 i =给出错误,说Can't assign to function call.如果column2/3不是函数,则为字段名称.那么,我该如何更新多个值,为什么在这里会给出错误呢?

I am not sure if the way I am passing values is correct. Also the Table.column2/3 i=gives error saying Can't assign to function call . Where column2/3 are not functions, they're field names. So, how do I update multiple values and why is it giving error here?

PS:我提到了sqlalchemy doc

PS: I referred the sqlalchemy doc

推荐答案

可以使用会话"将多个列更新为:

Multiple columns can be updated using the Session as:

def update_table(session, val1, val2, val3):
  session.query(Table).filter_by(col=val1).update(dict(col2=val2,col3=val3))
  session.commit()

这篇关于在Sqlalchemy中更新多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆