Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PostgreSQL如何追加执行查询的多个结果?_Python_Postgresql_Plpython - Fatal编程技术网

Python PostgreSQL如何追加执行查询的多个结果?

Python PostgreSQL如何追加执行查询的多个结果?,python,postgresql,plpython,Python,Postgresql,Plpython,我有一个函数getitems(id),它给出了与这个id相关的所有行 我在PostgreSQL中有一个名为func1的函数,它应该返回整个项目列表的getitems: CREATE OR REPLACE FUNCTION func1(listof_id integer[]) RETURNS SETOF newtype AS $BODY$ for item in listof_id: x=plpy.execute("SELECT * FROM getitems(%s)"%item

我有一个函数
getitems(id)
,它给出了与这个id相关的所有行

我在PostgreSQL中有一个名为func1的函数,它应该返回整个项目列表的
getitems

CREATE OR REPLACE FUNCTION func1(listof_id integer[])
  RETURNS SETOF newtype AS
$BODY$  

for item in listof_id:
    x=plpy.execute("SELECT * FROM getitems(%s)"%item)

return x;
$BODY$
  LANGUAGE plpythonu VOLATILE
  COST 100;
现在,它返回x,其中包含最后一次迭代的值(最后一个id在
listof_id
中的
getitems
结果)。如何修改它,使其将每个迭代附加到最后一个迭代

我试着做:

x={}
for item in listof_id:
    x+=plpy.execute("SELECT * FROM getitems(%s)"%item)

而且它不起作用…

id列表有多大?您可以修改/创建新的getitems以接受ID列表。未知。它可以有2个ID或10000个ID。修改现有功能是最后的选择。。。我宁愿避开它。
create or replace function func1(listof_id integer[])
  returns setof func_type as
$body$  

x = []
for item in listof_id:
    query = "select {0} as x, {0} * 2 as y, {0} * 3 as z, {0} * 4 as zz".format(item)
    result_set = plpy.execute(query)
    x.extend([[l['x'], l['y'], l['z'], l['zz']] for l in result_set])

return x
$body$ language plpythonu
;

select * from func1(array[1,2]);
 x | y | z | zz 
---+---+---+----
 1 | 2 | 3 |  4
 2 | 4 | 6 |  8