MySQL: Get column name or alias from query

后端 未结 10 1786
死守一世寂寞
死守一世寂寞 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:29

    Something similar to the proposed solutions, only the result is json with column_header : vaule for db_query ie sql.

    cur = conn.cursor()
    cur.execute(sql)
    res = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()]
    

    output json example:

    [
       {
          "FIRST_ROW":"Test 11",
          "SECOND_ROW":"Test 12",
          "THIRD_ROW":"Test 13"
       },
       {
          "FIRST_ROW":"Test 21",
          "SECOND_ROW":"Test 22",
          "THIRD_ROW":"Test 23"
       }
    ]
    

提交回复
热议问题