MySQL:从查询中获取列名或别名

时间:2011-02-15 22:01:18

标签: python mysql mysql-python

我不是要求SHOW COLUMNS命令。

我想创建一个与heidisql类似的应用程序,您可以在其中指定SQL查询,并在执行时返回包含表示查询结果的行和列的结果集。结果集中的列名称应与SQL查询中定义的所选列匹配。

在我的Python程序中(使用MySQLdb),我的查询只返回行和列结果,但不返回列名。在以下示例中,列名称为exttotalsizefilecount。 SQL最终将来自程序外部。

我能想到的唯一方法就是编写自己的SQL解析器逻辑来提取选定的列名。

是否有一种简单的方法来获取提供的SQL的列名? 接下来我需要知道查询返回了多少列?

# Python

import MySQLdb

#===================================================================
# connect to mysql
#===================================================================

try:
    db = MySQLdb.connect(host="myhost", user="myuser", passwd="mypass",db="mydb")
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

#===================================================================
# query select from table
#===================================================================

cursor = db.cursor ()   

cursor.execute ("""\
     select ext,
        sum(size) as totalsize,
        count(*) as filecount
     from fileindex
    group by ext
    order by totalsize desc;
""")

while (1):
    row = cursor.fetchone ()
    if row == None:
        break
    print "%s %s %s\n" % (row[0], row[1], row[2])

cursor.close()
db.close()      

9 个答案:

答案 0 :(得分:191)

cursor.description将为您提供一个元组元组,其中每个元组的[0]是列标题。

num_fields = len(cursor.description)
field_names = [i[0] for i in cursor.description]

答案 1 :(得分:24)

这与自由人相同,但更多的是使用列表和字典理解的pythonic方式

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

pprint.pprint(result)

答案 2 :(得分:10)

与@James的答案相似,更为抒情的方式可以是:

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

您可以在结果上获得包含地图的单个列:

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

或过滤结果:

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

或累积过滤列的值:

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

答案 3 :(得分:7)

我认为这应该做你需要的(建立在上面的答案)。我确信这是一种更灵活的方式来编写它,但你应该得到一般的想法。

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)

答案 4 :(得分:5)

您也可以使用MySQLdb.cursors.DictCursor。这将您的结果集转换为python词典的python列表,虽然它使用特殊的游标,因此在技术上不如接受的答案便携。关于速度不确定。这是使用此编辑的原始代码。

#!/usr/bin/python -u

import MySQLdb
import MySQLdb.cursors

#===================================================================
# connect to mysql
#===================================================================

try:
    db = MySQLdb.connect(host='myhost', user='myuser', passwd='mypass', db='mydb', cursorclass=MySQLdb.cursors.DictCursor)
except MySQLdb.Error, e:
    print 'Error %d: %s' % (e.args[0], e.args[1])
    sys.exit(1)

#===================================================================
# query select from table
#===================================================================

cursor = db.cursor()

sql = 'SELECT ext, SUM(size) AS totalsize, COUNT(*) AS filecount FROM fileindex GROUP BY ext ORDER BY totalsize DESC;'

cursor.execute(sql)
all_rows = cursor.fetchall()

print len(all_rows) # How many rows are returned.
for row in all_rows: # While loops always make me shudder!
    print '%s %s %s\n' % (row['ext'], row['totalsize'], row['filecount'])

cursor.close()
db.close()  

标准字典函数,例如,len(row[0])用于计算第一行的列数,list(row[0])用于列名列表(第一行)等。希望这有助于!

答案 5 :(得分:2)

看起来MySQLdb实际上并没有为该API调用提供翻译。相关的C API调用是mysql_fetch_fieldsthere is no MySQLdb translation for that

答案 6 :(得分:1)

尝试:

cursor.column_names

mysql连接器版本:

mysql.connector.__version__
'2.2.9'

答案 7 :(得分:0)

您还可以执行以下操作以获取字段标题:

table = cursor.description
check = 0
for fields in table:
    for name in fields:
        if check < 1:
            print(name),
        check +=1
    check =0

答案 8 :(得分:0)

与建议的解决方案类似,只有结果为printInstanceName的{​​{1}}的json,即column_header : vaule

db_query

输出json示例:

sql
相关问题