我目前正在开发一个项目,该项目需要一个可以使用sqlite3通过数据库访问信息(用户名,密码)的登录系统。这是当前的登录系统:
def UserLogin():
un = admin
pw = password
try:
statement = cur.execute("SELECT Username FROM Users")
for row in statement:
if un in row:
print("%s" % un)
pw_pas = cur.execute("SELECT Password FROM Users WHERE Username = %s" % (un))
if (pw in pw_pas):
print("Welcome\n")
elif pw not in pw_pas:
print("Password is incorrect")
return
print("Username is incorrect")
except IOError:
print("This process could not be executed")
print("login successful")
问题是,当我运行代码时,我收到一条错误消息“sqlite3.OperationalError:no such column:admin”。我已将用户名和密码输入数据库,但仍然出现此错误。
请帮忙
答案 0 :(得分:3)
不对SQL语句使用字符串替换。使用参数。
cur.execute("SELECT Password FROM Users WHERE Username = ?", (un,))