简体   繁体   English

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

[英]Retrieving Data from SQL Using pyodbc as list of dictionary

I have read the question "Retrieving Data from SQL Using pyodbc" and its answers. 我已经阅读了“使用pyodbc从SQL检索数据”问题及其答案。 I am able to retrieve DB data using pyodbc: 我可以使用pyodbc检索数据库数据:

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

However I get only rows of data, in each row which looks like 但是我只得到数据行,每一行看起来像

A, B, C
D, E, F

I would like it like a dictionary where the title of each column is also indicated for example 我希望它像一本字典,其中还指出了每列的标题

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

and so on, how can I get a list of dictionaries? 依此类推,如何获得字典列表?

You could try something like the following to get the expected results: 您可以尝试以下类似的方法以获得预期的结果:

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM