Python sqlalchemy存储过程执行的分析结果

Python sqlalchemy存储过程执行的分析结果,python,sqlalchemy,Python,Sqlalchemy,我在postgresql数据库中有一个存储过程 我正在尝试在PythonFlask应用程序中与sqlalchemy一起使用该函数。该查询如下所示: from sqlalchemy import func appts = db.session.execute(func.getopenappointments(current_user.id)) for appt in appts: # work with each appt ('(2,"2017-09-15 10:00:00",6,c

我在postgresql数据库中有一个存储过程

我正在尝试在PythonFlask应用程序中与sqlalchemy一起使用该函数。该查询如下所示:

from sqlalchemy import func

appts = db.session.execute(func.getopenappointments(current_user.id))

for appt in appts:
    # work with each appt
('(2,"2017-09-15 10:00:00",6,cleaning,available,5)',)
此查询的结果是类型为sqlalchemy.engine.result.ResultProxy的对象。该对象的每次迭代如下所示:

from sqlalchemy import func

appts = db.session.execute(func.getopenappointments(current_user.id))

for appt in appts:
    # work with each appt
('(2,"2017-09-15 10:00:00",6,cleaning,available,5)',)
问题是,我习惯于使用以下内容来指代专栏:

for appt in appts:
    print(appt.id)
但由于id不存在,此操作失败。我所意识到的是,输出几乎是一个字符串,我必须用python split()解析它,以获得我需要的值。如何将其保持为存储过程,但能够按列引用输出,或者至少作为元组而不是常规字符串引用输出?

请看一看。有一个名为
from_statement
的构造,可用于将SQL语句的结果解释为SQLAlchemy ORM模型。 因此,我假设您有一个
Appointment
类,它是一个ORM映射器,这可能是因为您使用了声明性的_base,也可能是因为您直接使用了映射器函数。 然后你可以做类似的事情

appts = db.session.query(Appointment).from_statement(func.getopenappointments(current_user.id))

这将运行SQL存储过程并解释结果(如果它是一组
Appointment
对象)。

为什么您的记录采用这种格式?我认为这是关键问题。该记录的格式与我期望的典型sqlalchemy结果不同。但这是我执行存储过程得到的结果。您知道结果中每个“字段”的数据类型吗。什么类型的“清洁”和“可用”?字符串?你在用什么数据库?如何定义
getopenappointment
?包括一个适当的。感谢您的反馈!我更新了问题的更多细节。@Ilja Everilälink已修复,对此表示抱歉