MySQL: Get column name or alias from query

后端 未结 10 1787
死守一世寂寞
死守一世寂寞 2020-11-28 20:04

I\'m not asking for the SHOW COLUMNS command.

I want to create an application that works similarly to heidisql, where you can specify an SQL query and w

10条回答
  •  我在风中等你
    2020-11-28 20:41

    Similar to @James answer, a more pythonic way can be:

    fields = map(lambda x:x[0], cursor.description)
    result = [dict(zip(fields,row))   for row in cursor.fetchall()]
    

    You can get a single column with map over the result:

    extensions = map(lambda x: x['ext'], result)
    

    or filter results:

    filter(lambda x: x['filesize'] > 1024 and x['filesize'] < 4096, result)
    

    or accumulate values for filtered columns:

    totalTxtSize = reduce(
            lambda x,y: x+y,
            filter(lambda x: x['ext'].lower() == 'txt', result)
    )
    

提交回复
热议问题