pypyodbc 无法使用 SQL Server Localdb 引用列名

pypyodbc Can't refer to column names using SQL Server Localdb

总 Python 菜鸟在这里。我正在针对 sql 服务器本地数据库编写脚本,无法根据列名调用值。

cnxn = pypyodbc.connect('Driver={SQL Server Native Client 11.0};Server=(localdb)\database;integrated security = true')
cursor = cnxn.cursor()
cursor.execute("SELECT username FROM [students].[dbo].[users]")
rows = cursor.fetchall()
for row in rows:
    print (row.username)
cnxn.close()

returns AttributeError: 'Row' 对象没有属性 'username'

print (row) 

按预期转储数据。并将代码修改为:

cursor.execute("SELECT username FROM [students].[dbo].[users]")
rows = cursor.fetchall()
x=0
for row in rows:
    print (row.cursor_description[x][0])
cnxn.close()

returns 每条记录的用户名。

为什么我打不通row.username?

使用print (row["username"])

pypyodbc does not support column properties as pyodbc 确实如此,只有一个列 collection 可以通过索引或它的 header 名称获得。

pyodbc 使用 row.attribute 语法,pypyodbc 使用 row["attribute"] 语法。