Postgresql 函数 plpython select json 值

Postgresql function plpython select json value

我正在使用 postgresql 9.6。 我建立了一个函数,我做了一个 "selec"t like :

id = TD["new"]["id"]
qry = plpy.prepare ("SELECT (decode->>'key')::integer AS key FROM table where id = ;", ["int"])
res= plpy.execute(qry,[id])

请求工作正常,但结果包含键和值,而不仅仅是值。 事实上,结果是这样的:{"key":2937}

我只想要值。

The result object emulates a list or dictionary object. The result object can be accessed by row number and column name.

create or replace function pf()
returns integer as $$

    query = 'select 1 as key'
    rs = plpy.execute(query)
    plpy.notice(rs.__str__())
    return rs[0]['key']

$$ language plpythonu;

select pf();                                                                                  
NOTICE:  <PLyResult status=5 nrows=1 rows=[{'key': 1}]>
 pf 
----
  1

https://www.postgresql.org/docs/current/static/plpython-database.html