2017-04-14 20 views
0

私はpostgresql 9.6を使用しています。 は、私は私のような「セレク」トン行う機能構築:Postgresql関数plpython select 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