如何从SQLAlchemy获取现有命名表的列类型

时间:2016-07-25 12:57:08

标签: python orm sqlalchemy

如何在SQLAlchemy中表示此查询?

string query = "SELECT SUM(See) AS [All]
 , (
       SELECT TOP 1 See
       FROM StateSite
       WHERE StatDate = @StatDate
   ) AS [Now]
 , (
       SELECT TOP 1 See
       FROM StateSite
       WHERE StatDate = @Yesterday
   ) AS [Last]
 , (
       SELECT SUM(See)
       FROM (
           SELECT TOP 7 *
           FROM StateSite
           ORDER BY id DESC
       )
   ) AS [week]
FROM StateSite";

2 个答案:

答案 0 :(得分:0)

Mb,我不明白你的意思是数据库类型。如果它是映射表,您可以像这样迭代列:

for c in foo.__table__.columns:
    print(c.type)

答案 1 :(得分:0)

def get_data_types(model):
'''return dictionary with column name as key and data type as value'''
    d = {}
    for c in model.__table__.columns:
        d[str(c.name)] = str(c.type)

    return d

其中model是sqlalchemy orm表。