如何在Python中获取PostgreSql函数的返回查询对象?

时间:2018-09-16 03:12:44

标签: python postgresql

具有如下定义的pgsql函数(ax1, ax2.......)

xaxis, yaxis

我试图在我的python代码中调用此函数,并期望返回的结果作为查询对象,而且我可以在查询对象中获得单个值。

#include <stdlib.h>

int main(int argc, char** argv)
{
    int* tmp_array = malloc(sizeof(int) * 2);
    tmp_array[0] = 1;
    tmp_array[1] = 2;
    tmp_array = realloc(tmp_array, 4);

    return 0;
}

但是,结果是SelRec(integer),就像没有存储任何数据一样,但它确实存在,如下所示:

-- select record
CREATE OR REPLACE FUNCTION SelRec(_sno integer)
  RETURNS SETOF app_for_leave AS
$BODY$
    BEGIN
    RETURN QUERY
        SELECT * FROM "app_for_leave" WHERE "sno"=_sno;
    END
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

我需要为每个单独的值import psycopg2 connection_string = 'postgresql://postgres:111111@localhost:5432/myDB' conn = psycopg2.connect(connection_string) cursor = conn.cursor() obj = cursor.execute("select * from SelRec(1)"); conn.commit() print obj # None 返回一个查询对象,我将进一步调用None sno | eid | ename | sd | ed | sid | status -----+-----+-------+------------+------------+-----+-------- 1 | 101 | | 2013-04-04 | 2013-04-04 | 2 | f

有人建议我如何实现这一目标?谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这可能是预期的行为。

从文档http://initd.org/psycopg/docs/cursor.html#cursor.execute

  

该方法返回无。如果执行查询,则返回值   可以使用fetch *()方法进行检索。

尝试类似的东西:

print(dir(obj), type(obj))
print(cursor.fetchall())

答案 1 :(得分:0)

我认为您应该尝试下面给出的查询。如要返回行集。

select * from SelRec(1) as result (sno int, eid int, ename varchar, sd data_type, ed data_type, sid int, status boolean)
相关问题