方法一
cursor = conn.cursor(pymysql.cursors.DictCursor) # 让mysql取出值从元组变成字典
方法二
def sqlToJson(cursor,sql,table_name):
cursor.execute(sql)
key = cursor.execute("select COLUMN_NAME from information_schema.columns where table_name=%s",(table_name,)).fetchall() # key为要输出的字典的key值列表(列名)
data=cursor.fetchall()
jsonList=[]
for i in data:
jsonList.append(dict(zip(key,i)))
return jsonList