使用python查询mysql数据库的时候,默认查询结果没有返回表字段名称,不方便使用。为了方便使用一般会选择将查询结果加上字段名称以字典组的方式返回查询结果。

实现如下:

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()
    ]

详细原理参考:https://docs.djangoproject.com/en/2.0/topics/db/sql/#executing-custom-sql-directly

 

相关文章:

  • 2021-11-07
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案