表Sqlalchemy Postgres中的更新值
我正在尝试更新表中的值,然后将其添加到数据库中。但是,当我做db.Session.commit
时,它似乎并没有更新表。
类别的示例
表:
类别(id = 1,catinfo =“ {}”,products =“ [{{}]”)
以下是我的步骤:
products2 = json.dumps([{'info': 'info'}])
# I am trying to update products in Category with an ID of 1 with a new list
db.engine.execute("UPDATE products FROM Category WHERE id = 1" + " SET products = " + products2)
# commit the database
db.session.commit()
我得到<代码> typeerror:dict不是序列不知道为什么
I am trying to update a value in my table, and add it back in into the database. However, it does not seem to be updating the table when I do db.session.commit
.
Example of Category
table:
Category(id=1, catInfo="{}", products = "[{}]")
Here are my steps:
products2 = json.dumps([{'info': 'info'}])
# I am trying to update products in Category with an ID of 1 with a new list
db.engine.execute("UPDATE products FROM Category WHERE id = 1" + " SET products = " + products2)
# commit the database
db.session.commit()
I am getting TypeError: dict is not a sequence
No idea why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如克里斯评论的那样,您需要编写查询以将数据插入数据库中。这是一个有关如何在SQLalchemy中插入对象的示例:
如果需要添加多个对象,请考虑
add_all
而不是。As Chris commented, you need to write a query to insert the data back in the database. Here is an example on how to insert an object in sqlalchemy:
If you need to add multiple objects, consider
add_all
instead. Reference