相关疑难解决方法(0)

如何修改新PostgreSQL JSON数据类型中的字段?

使用postgresql 9.3我可以选择JSON数据类型的特定字段,但是如何使用UPDATE修改它们?我在postgresql文档中或在线任何地方都找不到任何这样的例子.我试过了明显的事:

postgres=# create table test (data json);
CREATE TABLE
postgres=# insert into test (data) values ('{"a":1,"b":2}');
INSERT 0 1
postgres=# select data->'a' from test where data->>'b' = '2';
 ?column?
----------
 1
(1 row)
postgres=# update test set data->'a' = to_json(5) where data->>'b' = '2';
ERROR:  syntax error at or near "->"
LINE 1: update test set data->'a' = to_json(5) where data->>'b' = '2...
Run Code Online (Sandbox Code Playgroud)

postgresql json postgresql-9.3 postgresql-json

199
推荐指数
10
解决办法
18万
查看次数

如何使用python api代码仅更新postgres表中json类型列的1个属性

我有一个名为 products 的 postgresql(v.9.5) 表,使用 sqlalchemy 核心定义为:

products = Table("products", metadata,
                 Column("id", Integer, primary_key=True),
                 Column("name", String, nullable=False, unique=True),
                 Column("description", String),
                 Column("list_price", Float),
                 Column("xdata", JSON))
Run Code Online (Sandbox Code Playgroud)

假设表中的日期添加如下:

id |    name    |        description        | list_price |             xdata              
----+------------+---------------------------+------------+--------------------------------
 24 | Product323 | description of product332 |       6000 | [{"category": 1, "uom": "kg"}]
Run Code Online (Sandbox Code Playgroud)

使用API​​编辑代码如下:

def edit_product(product_id):
    if 'id' in session:
        exist_data = {}
        mkeys = []
        s = select([products]).where(products.c.id == product_id)
        rs = g.conn.execute(s)
        if rs.rowcount == 1:
            data = request.get_json(force=True)
            for r in …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy

5
推荐指数
1
解决办法
1700
查看次数