在Sqlalchemy中更新多列

编程入门 行业动态 更新时间:2024-10-26 16:23:54
本文介绍了在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)) sessionmit()

更多推荐

在Sqlalchemy中更新多列

本文发布于:2023-10-26 22:05:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sqlalchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!