尝试使用python对在sqlite 3中创建的数据库进行详细验证

ar5n3qh5  于 2023-03-13  发布在  SQLite
关注(0)|答案(1)|浏览(133)

我正在从在文本文档中保存和检查登录文件过渡到在SQL数据库中保存和检查登录文件。在我目前的解决方案中,我已经可以检查文件中的用户名和密码,然后报告用户名或密码是否正确。我的SQL实现代码如下:

def check_login_on_db():
        user = UserSignUpDetails(None, None, log_in_window_username_entry.get(),
                                 log_in_window_password_entry.get())
        enteredUsername = user.username
        enteredPassword = user.password

        conn = sqlite3.connect('database.db')
        cursor = conn.cursor()

        cursor.execute('SELECT * from SignUpDetails where username="%s"'%(enteredUsername))
        values = cursor.fetchall()

        for i in values:
            if values[i] == None:
                log_in_additional_info.config(text="This username is incorrect")
                conn.close()
            else:
                if values[i] == enteredUsername:
                    if values[i+1] != enteredPassword:
                        log_in_additional_info.config(text="The password is incorrect")
                        conn.close()
                    else:
                        log_in_additional_info.config(text="")
                        conn.close

我很感激你的帮助。
如果我输入了我知道是错误的细节,我希望得到错误消息,但是我没有。如果我使用这个实现,我让它工作并显示错误,但是我更希望它工作在告诉我哪个部分是错误的地方:

cursor.execute('SELECT * from SignUpDetails where username="%s"'%(enteredUsername))
        if cursor.fetchone():
           log_in_additional_info.config(
                            text="Success")
        else:
           log_in_additional_info.config(
                            text="Incorrect username or password")
0s0u357o

0s0u357o1#

我认为这里使用变量i来表示一行会使事情变得有点混乱,让我们重命名一些变量来帮助增加一些清晰度。
在我们清理的过程中,我将做一些额外的修改。首先,SQL不喜欢双引号,所以我们将引号翻转一下,这样SQL就可以看到单引号。其次,我将使用for语句的else子句(AKA no_break)来处理没有找到具有匹配密码的行的情况。
最后,顺便说一句,您的代码目前处理的是理论上两个帐户可以共享同一个用户名的情况。这是您真正想要的吗?如果不是,您可以执行fetchone(),然后摆脱for循环:

注,没有测试,只是从我的头上。

def check_login_on_db():
    user = UserSignUpDetails(
        None,
        None,
        log_in_window_username_entry.get(),
        log_in_window_password_entry.get()
    )

    enteredUsername = user.username
    enteredPassword = user.password

    with sqlite3.connect("database.db") as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT * from SignUpDetails where username = '%s'" % (enteredUsername))
            rows = cursor.fetchall()  # can two accounts actually have the same username?

    if not rows:
        log_in_additional_info.config(text="This username is incorrect")
        return

    for row in rows:
        if row[2] == enteredPassword:
            log_in_additional_info.config(text="")
            break
    else:
        log_in_additional_info.config(text="The password is incorrect")

如果您不喜欢这里的else子句,那么在这个示例中,您还可以执行以下操作:

def check_login_on_db():
    user = UserSignUpDetails(
        None,
        None,
        log_in_window_username_entry.get(),
        log_in_window_password_entry.get()
    )

    enteredUsername = user.username
    enteredPassword = user.password

    with sqlite3.connect("database.db") as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT * from SignUpDetails where username = '%s'" % (enteredUsername))
            rows = cursor.fetchall()  # can two accounts actually have the same username?

    if not rows:
        log_in_additional_info.config(text="This username is incorrect")
        return

    for row in rows:
        if row[2] == enteredPassword:
            log_in_additional_info.config(text="")
            return

    log_in_additional_info.config(text="The password is incorrect")

相关问题