【问题标题】:SQLAlchemy get output parameters from a postgresql stored procedureSQLAlchemy 从 postgresql 存储过程中获取输出参数
【发布时间】:2013-09-10 06:29:33
【问题描述】:

我正在使用 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; 

我的代码 sn-p 如下:

#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 之类的东西,并在我的代码中使用它。

有什么方法可以实现。工作代码 sn-p 将不胜感激。

非常感谢!

【问题讨论】:

    标签: stored-procedures sqlalchemy postgresql-9.2 output-parameter


    【解决方案1】:

    你可以试试这样的:

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

    这方面的灵感来自 SQLAlchemy 列表,其中有人询问了 similar question。这可能会产生所需的输出(我现在无法测试)。

    【讨论】:

    • 太棒了!非常感谢。还必须添加Column type
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2017-02-21
    • 2014-05-16
    • 2019-02-03
    • 2010-09-16
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多