Mysql-Python-Connector从存储过程中的select语句获取列名

时间:2015-12-01 20:59:08

标签: mysql mysql-python

我有一个包含多个语句的存储过程,简化如下

create temp table...; 
update temp table...; 
....
select * from temp table; #last statement

我有以下Python代码来获取SP的返回结果。我需要列名,以便我可以将结果转换为JSON格式。但是,cursor.description不会返回任何我期望的内容。建议请!! Mysql5.7,mysql-python-connector 2.x由Oracle,python 3.5

组成
cursor.callproc(proc, args)
columns = cursor.description
print(columns) #returns [('@_sp_get_toc_arg1', 8, None, None, None, None, 1, 128)]

for result in cursor.stored_results():
    return result.fetchall()

1 个答案:

答案 0 :(得分:0)

cursor.stored_results()返回的迭代器会生成游标,您需要检查那些游标的描述以获取列名,而不是初始游标:

for result in cursor.stored_results():
    print(result.description)
    return result.fetchall()
相关问题