I am using postgresql function and calling from python. but I can't fetch cursor.
this is my postgresql function.
CREATE LANGUAGE plpgsql;
create or replace FUNCTION getPerson() returns REFCURSOR
AS $$
DECLARE
p_cursor refcursor;
BEGIN
open p_cursor FOR
select m.name AS name, m.surname AS surname from person m;
RETURN p_cursor;
END; $$
LANGUAGE plpgsql;
this is my python function
import psycopg2
try:
conn = psycopg2.connect(database="dt", user = "us", password = "pass", host = "127.0.0.1", port = "5432")
cur = conn.cursor()
res = cur.callproc('getPerson')
result = cur.fetchall()
for row in result:
print "printed row"
print row
conn.commit()
cur.close()
conn.close()
except:
print "error"
RESULT:
printed row
('<unnamed portal 1>',)
fetch all from "<unnamed portal 1>"(use name returned by function) and then fetch your data from it.