首页 > 解决方案 > 在 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)

标签: pythonauthentication

解决方案


我终于做对了。我使用了 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))```

推荐阅读