小编edv*_*n22的帖子

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

总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?

python sql-server localdb

4
推荐指数
1
解决办法
1633
查看次数

标签 统计

localdb ×1

python ×1

sql-server ×1