SQLAlchemy get output parameters from a postgresql stored procedure

可紊 提交于 2019-12-11 03:57:21

问题


I am using Postgresql9.2 and SQLAlchemy0.8 . I have a stored procedure in database which has many out parameters and i want them to use by dot notation. But so far I have failed miserably. Below is how my code looks like.

An example to show what i am doing is as follows.

CREATE OR REPLACE FUNCTION stored_proc_name(IN in_user_id bigint, IN in_amount bigint,
OUT pout_one bigint, OUT pout_two bigint )
      RETURNS record AS
$BODY$
begin
    select count(*) as AliasOne into pout_one from tabe_names where conditions;
    select user_name as AliasTwo into pout_two from table_name where condition;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION stored_proc_name(bigint, bigint)
  OWNER TO postgres; 

My code snippet is as following:

#session object from sessionmaker
result_obj = session.execute(func.stored_proc_name(user_id, amount))
print result_obj.fetechall()
#The above print statement prints following on the console.
>> [('(1,100)',)]

Clearly the above result fetches string. What I want is something like result_obj.pout_one and use it in my code.

Is there any way of achieving it. A working code snippet will be highly appreciated.

Thanks a lot!


回答1:


You could try something like this:

query = select([column('pout_one'), column('pout_two')], 
               from_obj=[func.stored_proc_name(user_id, amount)])
session.execute(query)

The inspiration for this is from the SQLAlchemy list where someone asked a similar question. This could result in the desired output (I can't test it right now).



来源:https://stackoverflow.com/questions/18712049/sqlalchemy-get-output-parameters-from-a-postgresql-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!