如何创建Python SQLite登录并创建对数据库验证的帐户,然后提示用户另一个数据库



我正在为我的课程做准备,我不知道如何创建一个将针对数据库验证的登录数据库...创建帐户位应在我的专栏和密码必须足够强。一旦我进行了分类,我想将用户引导到我已经创建的另一个数据库...我正在考虑将其他数据库导入到登录SQL文件中。如果你们能帮助我,我会很感激。.目前它不起作用,但这是我所拥有的,还没有完成,但我猜这是非常错误的...:

import sqlite3 as lite
import sys
def user_menu():
print("Login menu")
print()
print("1. Login")
print("2. Create Account")
print("0. Exit Program")
def get_user_menu_choice():
accepted = False
while not accepted:
    choice = int(input("Please select an option: "))
    if 0 <= choice <= 2:
        accepted = True
    else:
        print("Pleae enter a valid value:")
return choice
def query(sql,data):
with sqlite3.connect(DATABASE) as db:
    cursor = db.cursor()
    cursor.execute(sql,data)
    db.commit()
def insert_data(values):
sql = "insert into Product (name,password) values(?,?)"
query(sql,values)
def main():
finished = False
while not finished:
    user_menu()
    choice = get_user_menu_choice()
    if choice == 1:
        # Get login details from user
        user = input('User: ')
        password = getpass.getpass('Password: ')
        # Connect to database
        db = sqlite3.connect('DATABASE') 
        c = db.cursor()
        # Execute sql statement and grab all records where the "usuario" and
        # "senha" are the same as "user" and "password"
        c.execute('SELECT * FROM Product', (user, password))
        # If nothing was found then c.fetchall() would be an empty list, 
which
        # evaluates to False 
        if c.fetchall():
            print('Welcome')
        else:
            print('Login failed')
    elif choice == 2:
        name = input("Please enter name of name ")
        password = input("Please enter the passsword {0}: ".format(name))
        insert_data((name,password))
    elif choice == 0:
        finished = True
    else:
        finished = True
if __name__ == "__main__":
DATABASE = "userlogins.db"
main()
def insert_data(values):
    sql = "insert into Product (name,password) values(?,?)"
    query(sql,values)
here what is query(sql,values) for??
here you passing only one argument to function insert_data
and here you are calling this function with two arguments.
so it would not insert data
elif choice == 2:
    name = input("Please enter name of name ")
    password = input("Please enter the password {0}: ".format(name))
    insert_data((name,password))

因此,在DEF INSERT_DATA的定义中,您应该精确编写

def insert_data(value1,value2): sql ="插入产品(名称,密码)值(?,?)" cursor.excecute(sql)

最新更新