python3 利用pysql将sql取出数据转为字典列表类型

直接上代码

def sqlToJson(cursor,sql,key):
    cursor.execute(sql)
    data=cursor.fetchall()
    jsonList=[]
    for i in data:
        jsonList.append(dict(zip(key,i)))
    return jsonList

cusrsor是pymysql的Cursor对象,sql为sql语句类型为字符串,一般是查询语句,key为要输出的字典的key值列表

猜你喜欢

转载自blog.csdn.net/qq_36797488/article/details/108284860