我通过SQL Alchemy查询我的数据库,如下所示:
dev_engine = create_engine('mysql://...')
get_prod_instrument = "select * from market_instrument inner join exchange_instrument on market_instrument.id = exchange_instrument.instrument_id \
inner join tick_size on market_instrument.id = tick_size.instrument_id \
left join futures on market_instrument.id = futures.instrument_id \
inner join (select distinct(substring(m.symbol, 1, 2)) as product from market_instrument as m inner join fills as f on m.id=f.instrument_id where symbol<>\"\" and fill_timestamp between CURDATE() - INTERVAL " + str(last_days) + \
" DAY AND NOW()) r on market_instrument.symbol like concat(r.product, '%') \
where symbol not like '%_old%' and symbol not like '%:%';"
result = dev_engine.execute(get_prod_instrument)
result
是一张大表。我希望能够在sqlalchemy中使用此表的列名。有没有办法调整我的查询或使用Python来获取某些python对象中的列名?
答案 0 :(得分:0)
我能够像这样得到列名:
dev_engine = create_engine('mysql://...')
get_prod_instrument = "select * from market_instrument inner join exchange_instrument on market_instrument.id = exchange_instrument.instrument_id \
inner join tick_size on market_instrument.id = tick_size.instrument_id \
left join futures on market_instrument.id = futures.instrument_id \
inner join (select distinct(substring(m.symbol, 1, 2)) as product from market_instrument as m inner join fills as f on m.id=f.instrument_id where symbol<>\"\" and fill_timestamp between CURDATE() - INTERVAL " + str(last_days) + \
" DAY AND NOW()) r on market_instrument.symbol like concat(r.product, '%') \
where symbol not like '%_old%' and symbol not like '%:%';"
result = dev_engine.execute(get_prod_instrument)
# getting columns
description_tuple = result.cursor.description
headers = [k[0] for k in description_tuple] # a list of the column names
result.cursor.description
是一个元组元组,每个元组的第一个属性是列名
我不认为这是最好的解决方案,因为这是对ResultProxy对象的一个底层对象的直接访问,而不是通过getter检索对象,但我找不到任何封装不使用声明性语法时检索列名的方法。