python - SQLAlchemy-执行存储过程并填充类
问题描述
有没有办法执行存储过程并取回一个类?
我努力了:
session.query(MyClass).from_statement(...)
其中 myclass 定义为:
MyClass(Base):
id = Column(Integer)
name = Column(String)
...
我知道我应该放:
__tablename__ ‘sometable’
但是没有表,因为所有行都是存储过程调用的结果。你知道有什么办法可以解决这个问题吗?
解决方案
迈克尔拜耳回购所有者
SQLAlchemy 目前不支持除 Oracle 之外的任何后端的 OUT 参数。为了现在使用 OUT 参数,您需要使用 DBAPI 函数 callproc(),请参阅http://initd.org/psycopg/docs/cursor.html#cursor.callproc。
您的函数应如下所示:
def execproc(procname, engine, queryParams=[]):
connection = engine.raw_connection()
cursor = connection.cursor()
try:
cursor.callproc(procname, queryParams)
rows = list(cursor.fetchall())
cursor.close()
connection.commit()
return rows
finally:
connection.close()
推荐阅读
- php - 如何在php中设置水印图像
- javascript - 在窗口和标签面板 ExtJs 中显示相同的面板
- node.js - Mongoose 中的更新问题
- http-status-code-404 - 404 not found error without https:// 当键入 https 时,我认为它工作正常 cloudflare 问题
- reactjs - redux-saga 与 redux 一起使用会导致 render() 被调用两次
- apache-beam - 如何使用 Apache Beam 合并两个流并对合并的流执行有状态操作
- flutter - Bloc 测试中 BehaviourSubject 的断言问题
- ios - UICollectionView 中的选择和取消选择问题
- java - 如何为预期的异常测试方法添加多个案例?
- sql - 所有插入查询的单个存储过程