使用sqlalchemy获取特定数据的列名

2024-04-26 18:33:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是sqlalchemy 0.8,我只想获取输入的列名,而不是表中的所有列。你知道吗

代码如下:

rec = raw_input("Enter keyword to search: ")
res = session.query(test.__table__).filter(test.fname == rec).first()

data = ','.join(map(str, res)) +","
print data

#saw this here @ SO but not the one I wanted. It displays all of the columns
columns = [m.key for m in data.columns]
print columns

Tags: columnstheto代码testinputsearchdata
1条回答
网友
1楼 · 发布于 2024-04-26 18:33:32

您只需查询所需的列。就像你有个模特MyModel 你可以做:

session.query(MyModel.wanted_column1, ...) ...  # rest of the query

这将只选择此处提到的所有列。你知道吗

您可以使用select语法。你知道吗

或者,如果仍然希望返回模型对象而不加载某些列,则可以使用deferred column loading。你知道吗

相关问题 更多 >