sqlalchemy 查询:结果列名
Posted
技术标签:
【中文标题】sqlalchemy 查询:结果列名【英文标题】:sqlalchemy query: result column names 【发布时间】:2016-02-16 13:59:19 【问题描述】:我有一个名为 DevicesGpsTelemetry
的模型(使用声明式基础声明)。我这样查询:
models = session.query(
DevicesGps.ReceivedDateUtc,
DevicesGps.ReceivedTimeUtc,
DevicesGps.Latitude,
DevicesGps.Longitude)
它呈现为:
SELECT
devices_gps."ReceivedDateUtc" AS "devices_gps_ReceivedDateUtc",
devices_gps."ReceivedTimeUtc" AS "devices_gps_ReceivedTimeUtc",
devices_gps."Latitude" AS "devices_gps_Latitude",
devices_gps."Longitude" AS "devices_gps_Longitude"
FROM devices_gps
我的问题:如何将 AS 语句之后的名称(如 "gps_telemetry_ReceivedDateUtc"
)更改为我想要的名称?
背景:这些名字对我来说很重要,因为我用这个查询 pandas.read_sql
并且这些名字变成了 DataFrame 的列名
【问题讨论】:
【参考方案1】:在每列后添加.label('desired_name')
。在你的情况下,它看起来像
models = session.query(
DevicesGps.ReceivedDateUtc.label("gps_telemetry_ReceivedDateUtc"),
DevicesGps.ReceivedTimeUtc.label("gps_telemetry_ReceivedTimeUtc"),
DevicesGps.Latitude.label("gps_telemetry_Latitude"),
DevicesGps.Longitude.label("gps_telemetry_Longitude")
)
【讨论】:
谢谢。在文档中不知何故错过了这一点以上是关于sqlalchemy 查询:结果列名的主要内容,如果未能解决你的问题,请参考以下文章
使用 SQL 和 Sqlalchemy 查询的表具有相同的列名