首页 > 解决方案 > 在python中迭代数据库的问题

问题描述

我正在学习编程,但我遇到了一些问题。如果有人能指出我做错了什么,那就太好了。我想编写一个小程序来执行以下操作:

我已经设法以粗体显示所有内容。验证密码的逻辑似乎不正确。只有当我按照我在数据库中存储帐户的相同顺序输入凭据时,验证才有效。下面是该表的副本。

在此处输入图像描述

在我的示例中,我将使用表中的最后一个帐户并查看发生了什么。 在此处输入图像描述

在下面找到我的代码副本:

import sqlite3

connect_fworlddb = sqlite3.connect('fworld.db')
myCursor = connect_fworlddb.cursor()
myCursor.execute('SELECT * FROM account_tbl')
allusers = myCursor.fetchall()

for chances in range(3,0,-1):

    xrole = input("Enter your Role: ")
    xuserid = input("Enter your UserID: ")
    xpassword = input("Enter your Password: ")

    for user in allusers:
        print(user) #i have added this part to print what's happening
        if user[2]==xrole and  user[0]==xuserid and user[1]==xpassword in user:
            print("Found")
            break
        else:
            print("Not Found")
if chances == 1:
    print("User" + " " + xuserid + " " + "account has been locked.")
    print("Please contact the system Administrator on ext. 5408")

标签: python-3.x

解决方案


推荐阅读