【发布时间】:2015-12-31 20:07:50
【问题描述】:
我正在使用 SQLAlchemy 查询 sqlite db,如下所示:
import db
...
results = session.query(table).all()
for result in results:
print result
print "how to print column name?"
这是一个来自 db 类的 sn-p:
class Device(Base):
__tablename__ = "devices"
name = Column(String(250), primary_key=True)
location = Column(String(250), nullable=False)
def __init__(self, name, location):
self.name = name
self.location = location
尝试根据文档在结果上使用“.column_descriptions”会引发“'list' object has no attribute 'column_descriptions'”错误。
动态获取列名和值的正确方法是什么?我想要这个,所以我可以构建一个函数来处理所有查询的 json 转换,而不是到处重复代码。
【问题讨论】:
-
这个问题stackoverflow.com/questions/8947616/… 记录了
result.keys()。也就是说,你可能想要什么。
标签: python sql sqlite python-2.7 sqlalchemy