总Python noob在这里。我正在针对sql server本地db编写脚本,并且无法基于列名调用值。
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()
Run Code Online (Sandbox Code Playgroud)
返回AttributeError:“行”对象没有属性“用户名”
print (row)
Run Code Online (Sandbox Code Playgroud)
按预期转储数据。并将代码修改为:
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()
Run Code Online (Sandbox Code Playgroud)
返回每个记录的用户名。
为什么我不能调用row.username?