我不熟悉Python中的数据库,为了练习一些关键技能,我构建了一个登录屏幕,将用户名和散列密码写入数据库,然后根据数据库中的内容检查用户的输入。但是,当我试图从数据库中提取用户名和密码并将它们存储在变量中时,我总是得到“'NoneType'object is not subscriptable”错误。
我该怎么解决这个问题?
这是我的程序中包含错误的函数def login():
usernameAttempt = input("Enter your username: ")
passwordAttempt = input("Enter your password: ")
usernameSql = """SELECT Username FROM Details WHERE Password = '%s'""" % (passwordAttempt) # Selecting username in database
cur.execute(usernameSql)
usernameSql = cur.fetchone()
userFetched = usernameSql[0] # Pulling the item from the database
passwordSql = """SELECT Password FROM Details WHERE Username = '%s'""" % (usernameAttempt)
cur.execute(passwordSql)
passwordSql = cur.fetchone()
passFetched = passwordSql[0]
dbPassword, dbSalt = passFetched.split(":")
return dbPassword, dbSalt, passwordAttempt, usernameAttempt, userFetched, passFetched
我希望将userFetched和passFetched变量分配给username和password列中索引0处的值,但错误消息如下:
回溯(最近一次呼叫):
文件“C:\Users\laurer\Documents\Login屏幕.py“,第57行,英寸
dbPassword,dbSalt,passwordattest,用户名尝试,userFetched,passFetched=login()
文件“C:\Users\laurer\Documents\Login屏幕.py“,第34行,登录
userFetched=usernameSql[0]#正在从数据库中提取项
TypeError:“NoneType”对象不可订阅