【问题标题】:Iterate each column of SQLAlchemy ResultProxy迭代 SQLAlchemy ResultProxy 的每一列
【发布时间】:2018-08-03 06:19:33
【问题描述】:

如何迭代resultproxy 对象的每一列?

我已经尝试了来自question的答案:

for col in class_mapper(obj.__class__).mapped_table.c)

得到这个错误:

sqlalchemy.orm.exc.UnmappedClassError: Class 'sqlalchemy.engine.result.RowProxy' is not mapped

【问题讨论】:

标签: python sqlalchemy


【解决方案1】:

RowProxy 不是映射类,因为错误告诉您。它代表较低级别的抽象,并“直接”对数据库结果行进行建模——不涉及 ORM。 Reading the documentationRowProxy 部分类似于有序字典,并提供映射接口,因此您可以使用keys()items() 分别迭代键和键、项元组。如果您遍历 RowProxy 实例,您将获得值。

所以如果你想遍历一行的键、值元组:

for row in result:
    for key, value in row.items():
        pass

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多