python执行查询sql的结果转为列表套字典

1. 代码(参考:https://www.cnblogs.com/xiao-xue-di/p/11840699.html)

def dict_fetchall(cursor):
    "Return all rows from a cursor as a dict"
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
    ]

 

posted @ 2021-09-25 14:10  10132714  阅读(477)  评论(0编辑  收藏  举报