2017-04-14 30 views
0

我正在使用postgresql 9.6。 我建立一個函數在那裏我做了一個「選擇素」 T這樣的:Postgresql函數plpython選擇json值

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

請求做工精細,但結果保持鍵和值,不僅看重。 實際上,結果是這樣的:{「key」:2937}

我只想要這個值。

回答

0

結果對象模擬列表或字典對象。結果對象可以通過行號和列名訪問。

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