Python 访问 sql 中的列,在阅读时不使用数字

Python access columns from sql without using number when reading

我正在使用 pyodbc 提取数据并像这样处理我检索的行

   data=cursoor(fetchall())
   userlist=list(data)
   
for user in userlist
    if user[3]

一切正常,但我想知道如何做同样的事情,但使用列名而不是 [3] 示例行 ["AccountName"] 或更好 user.AccountName。这是一个简单的脚本,所以我不想过度设计,但希望它更具可读性。

the wiki 中所述,pyodbc 已经支持您描述的内容:

cursor.execute("select album_id, photo_id from photos where user_id=1")
row = cursor.fetchone()
print(row.album_id, row.photo_id)
print(row[0], row[1])  # same as above, but less readable