当前位置: 代码迷 >> 综合 >> python3 利用pysql将sql取出数据转为字典列表类型
  详细解决方案

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

热度:73   发布时间:2023-10-29 03:45:31.0

直接上代码

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值列表