如何解决如何使用 MSSQL 和 Python 将元素与用户索引进行比较
我在 Microsoft SQL Server Management Studio 中创建了我的表,然后我在 Python 中创建了与数据库的连接。
我可以获取一个表的全部记录,这意味着我的连接没有任何问题。 但是我想创建一个登录系统,我试图比较用户输入的输入和登录系统的数据库记录。
这是我的代码
def read(conn,name,password):
cursor = conn.cursor()
cursor.execute("select * from UserList")
if(name==cursor["Username"] & password == cursor["Password"]) :
print("A")
因此,当我运行它时,它给出了一个错误
File "C:/Users/PycharmProjects/MyProject/Project",line 18,in <module>
read(conn,password)
File "C:/Users/PycharmProjects/MyProject/Project",line 6,in read
if(name==cursor["Username"] & password == cursor["Password"]) :
TypeError: 'pyodbc.Cursor' object is not subscriptable
问题是什么,我应该遵循哪个指南来解决这个问题?
解决方法
您需要使用这些游标函数之一将查询结果加载到内存中。
cursor.execute("select * from UserList")
# get all results
results = cursor.fetchall()
# get one result
results = cursor.fetchone()
# get multiple results in chunks
results = cursor.fetchmany(10)
参考:
https://github.com/mkleehammer/pyodbc/wiki/Cursor
"fetchone" 和 "fetchmany" 会将结果分块加载到内存中。然后您需要循环直到没有更多可用记录。
while True:
results = cursor.fetchmany(10)
if len(results)==0:
break
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。