使用 pyodbc 作为字典列表从 SQL 检索数据

Retrieving Data from SQL Using pyodbc as list of dictionary

我已阅读问题 "Retrieving Data from SQL Using pyodbc" 及其答案。我可以使用 pyodbc 检索数据库数据:

for row in cursor.fetchall():
    print( row )

但是我只得到数据行,每一行看起来像

A, B, C
D, E, F

我希望它像一本字典,其中还标明了每一列的标题,例如

studentName:A, studentAge:B, studentGrade: C
studentName:D, studentAge:E, studentGrade: F

等等,如何获取词典列表?

您可以尝试如下操作以获得预期结果:

for row in cursor.fetchall():
  print(row.studentName, row.studentAge, row.studentGrade)

希望对您有所帮助。