stored-procedures - SQLAlchemy 从 postgresql 存储过程获取输出参数

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

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

相关文章:

MySQL-列中值的所有子集

mysql - 在 MySQL 存储过程中选择不返回值

PostgreSQL - 错误 - 无法打开文件 "C:\Insert_postgres.csv"以读取 : No such file or directory

postgresql - 错误: no unpinned buffers available in postgres

sql-server - 从存储过程到 REST 服务

Mysql 程序更新所有记录的两个日期之间的差异

python - 关于 sqlalchemy 中的 unique=True 和 (unique=True, index=True)

json - 使用 Pandas .to_sql 将 JSON 列写入 Postgres

python - 如何在 SQLAlchemy 中编写生成更新

java - JPA Eclipselink query.first Result() 和 query.setMaxResults() 与 Postgres