我正在使用 Postgresql9.2 和 SQLAlchemy0.8 。我在数据库中有一个存储过程,其中有许多输出参数,我希望它们通过点表示法使用。但到目前为止我已经惨败了。下面是我的代码的样子。
展示我正在做的事情的示例如下。
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;
我的代码片段如下:
#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)',)]
显然上面的结果获取了字符串。我想要的是类似于 result_obj.pout_one
的东西,并在我的代码中使用它。
有什么办法可以实现吗?工作代码片段将受到高度赞赏。
非常感谢!
最佳答案
你可以尝试这样的事情:
query = select([column('pout_one'), column('pout_two')],
from_obj=[func.stored_proc_name(user_id, amount)])
session.execute(query)
这个的灵感来自于 SQLAlchemy 列表,其中有人询问了 similar question 。这可能会产生所需的输出(我现在无法测试它)。
关于stored-procedures - SQLAlchemy 从 postgresql 存储过程获取输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18712049/