python - 在 Python 中通过循环验证登录用户
问题描述
我试图通过确保只有具有部门的注册用户才能访问应用程序中的部门模板来验证用户。我想要这样一种情况,一旦用户输入他的登录详细信息,系统就会检查他的部门,并通过他的登录详细信息,打开他部门的模板。因此,应用程序的工作是通过他的登录详细信息识别他所在的部门并为他打开正确的模板。
如果在第一个部门中找不到他,我希望系统循环遍历其他部门,以查看他是否在其他部门中注册。这是我的代码:
def verifyUser(self):
self.db_connection()
try:
username = self.user.text()
password = self.pwtx.text()
result = self.conn.execute(
"""SELECT *
FROM newUser_general
WHERE userID = ? AND dept = 'Comp Dept' """,
(username, password)
) # include dept
print("SQL Statement executed")
if (len(result.fetchall()) > 0):
print("user found")
self.compDept()
else:
print("user not found")
except Exception as err:
print(err)
解决方案
我终于做对了。我使用了 if 和 else 条件语句。首先我实例化了像 dept1 = "Comp sci", dept2 = "Statistics" 这样的部门然后我做了
```if dept1:
result = self.cursor.connect(result = self.conn.execute(
"""SELECT *
FROM newUser_general
WHERE userID = ? AND dept = 'Comp Dept' """,
(username, password))
else if dept2:
result = self.cursor.connect(result = self.conn.execute(
"""SELECT *
FROM newUser_general
WHERE userID = ? AND dept = 'Statistics' """,
(username, password))```
推荐阅读
- javascript - 等待 fetch() 解决使用 Jest 进行 React 测试?
- python - How to set a probability of a value becoming a zero for an np.array?
- python-3.x - python statsmodel seasonal_decompose Error with “len() of unsized object”
- flutter - Read state from outside of Widget
- visual-studio-code - Why I'm getting diff node.js versions reported?
- c# - Send JSON data to REST API with sub payloads
- c++ - How to check that two arbitrary memory ranges are not overlapped in c/c++
- php - 由于 PHP、Apache 版本导致的 PHP 错误?
- java - 使用 Java 客户端在 Google 数据存储上的 CompositeFilter 的 CompositeFilter
- unity3d - Unity-在滚动背景上向左移动对象