表Sqlalchemy Postgres中的更新值

发布于 2025-02-08 20:47:55 字数 524 浏览 0 评论 0原文

我正在尝试更新表中的值,然后将其添加到数据库中。但是,当我做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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2025-02-15 20:47:55

正如克里斯评论的那样,您需要编写查询以将数据插入数据库中。这是一个有关如何在SQLalchemy中插入对象的示例:

customer = Customer(
    first_name='Todd',
    last_name='Birchard',
    email='[email protected]',
    preferred_language='English',
    join_date=datetime.now()
)
session.add(customer)
session.commit()

如果需要添加多个对象,请考虑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:

customer = Customer(
    first_name='Todd',
    last_name='Birchard',
    email='[email protected]',
    preferred_language='English',
    join_date=datetime.now()
)
session.add(customer)
session.commit()

If you need to add multiple objects, consider add_all instead. Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文