1

With pgsql function SelRec(integer) as defined below:

-- 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;

I tried to call this function inside my python code, and expected the returned result as query object which furthermore I could get an individual value inside the query object.

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

Yet, the result is None as if there is no data stored, but it does exist like below:

 sno | eid | ename |     sd     |     ed     | sid | status 
-----+-----+-------+------------+------------+-----+--------
   1 | 101 |       | 2013-04-04 | 2013-04-04 |   2 | f

My need is the obj return an query object which I would further call DataSet = obj.first(), and DataSet.status, ect for each individual value.

Any suggestion how I can achieve that? Thanks.

2
  • Ah my bad. Updated my answer. Commented Sep 16, 2018 at 4:31
  • Thanks, I got it, @jmunsch, the object return as turple, wondering if it is possible to access each value by column name instead of index 0,1,2.. Can we archive that just like using sqlalchemy? Thanks. Commented Sep 16, 2018 at 4:54

2 Answers 2

1

I think it might be expected behavior.

From the docs http://initd.org/psycopg/docs/cursor.html#cursor.execute

The method returns None. If a query was executed, the returned values can be retrieved using fetch*() methods.

Try something like:

print(dir(obj), type(obj))
print(cursor.fetchall())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, after I ran command obj.fetchall(), I got an exception AttributeError: 'NoneType' object has no attribute 'fetchall'. I'm pretty sure my function ` SelRec(1)` return a result when I run it in pgSql select * from SelRec(1).
0

I think you should try query given below. As you want to return set of rows.

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

12 Comments

Thanks so much, does this statement run in Postgresql? I think I need it executed in python code, :)
If this return set of rows in postgresql then I think it should run in python also with fetchall
Also get error running in postgresql, ERROR: a column definition list is only allowed for functions returning "record"
Does * in select query means all the column stated in as result section??
Meaning it is not possible I could achieve that expectation. Understand cursor.execute(query_string) always return a tuple initd.org/psycopg/docs/cursor.html#fetch . Tone of thanks for your help on this :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.