MySQL: Get column name or alias from query

后端 未结 10 1756
死守一世寂寞
死守一世寂寞 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条回答
  •  -上瘾入骨i
    2020-11-28 20:32

    I think this should do what you need (builds on the answer above) . I am sure theres a more pythony way to write it, but you should get the general idea.

    cursor.execute(query)
    columns = cursor.description
    result = []
    for value in cursor.fetchall():
        tmp = {}
        for (index,column) in enumerate(value):
            tmp[columns[index][0]] = column
        result.append(tmp)
    pprint.pprint(result)
    

提交回复
热议问题