Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用数据库中的文本作为用户名和密码进行身份验证?_Python_Authentication - Fatal编程技术网

Python 如何使用数据库中的文本作为用户名和密码进行身份验证?

Python 如何使用数据库中的文本作为用户名和密码进行身份验证?,python,authentication,Python,Authentication,我的userpass.txt如下所示: 加比尔,宝贝 萨比尔桥 马比尔,勃朗特 我如何使用文本文件的左栏与文本文件右栏上的单词关联来访问,而不是使用bonjovi作为用户名,isagod作为密码?这是最基本的代码,因为我15岁,还在上学 import time import sqlite3 conn = sqlite3.connect("UserPass.db") cursor = conn.cursor() print("*"*50) print("Authentication Databa

我的userpass.txt如下所示:

加比尔,宝贝

萨比尔桥

马比尔,勃朗特

我如何使用文本文件的左栏与文本文件右栏上的单词关联来访问,而不是使用bonjovi作为用户名,isagod作为密码?这是最基本的代码,因为我15岁,还在上学

import time
import sqlite3
conn = sqlite3.connect("UserPass.db")
cursor = conn.cursor()

print("*"*50)
print("Authentication Database")
print("*"*50)

cursor.execute("""
CREATE TABLE tblUserPass
(
    usernames TEXT,
    passwords TEXT,
    primary key (usernames)
)"""
               )
print("tblUserPass created in UserPass.db")

def readTextFile(userFile):
    numRecs = 0
    userDBRec = []
    userTextRec = userFile.readline()
    while userTextRec != "":
        numRecs += 1
        field = userTextRec.split(",")
        usernames = field[0]
        passwords = field[1]
        print(usernames, passwords)
        userDBRec.append(usernames)
        userDBRec.append(passwords)
        cursor.execute ("insert into tblUserPass VALUES (?,?)", userDBRec)
        conn.commit()

        userDBRec = []
        userTextRec = userFile.readline() 
    return numRecs

userFile = open("UserPass.txt", "r")
numRecs = readTextFile(userFile)
print("\n",numRecs, "records transferred")
userFile.close()

for rows in cursor.execute('SELECT * FROM tblUserPass'):
    print(rows[1])

username=input("enter user")
password=input("enter pass")

while username!='bonjovi' or password!='isagod':
    print("one of 'em incorrect")
    time.sleep(1)
    print("go again")
    username=input("ENTER USER AGAIN")
    password=input("ENTER PASS AGIAN")

print("hento and welcom")

首先,您不应该在数据库中以明文形式存储密码

这实际上是一个简单的数据库查询

username=input("enter user")
password=input("enter pass")

while cursor.execute(f"SELECT * FROM tblUserPass where usernames='{username}' AND passwords='{password}'").fetchone() is None:
    print("one of 'em incorrect")
    time.sleep(1)
    print("go again")
    username=input("ENTER USER AGAIN")
    password=input("ENTER PASS AGIAN")

print("hento and welcom")
查询将搜索与用户名和密码匹配的行

最后,欢迎来到SO和bravo,主动从头开始写东西

编辑


在此之前,当您初始化数据库时,这是一个小问题,当您执行
userTextRec=userFile.readline()
时,末尾实际上有一个换行符,因此您的所有密码(最后一行除外)都以
\n
作为后缀。要删除它,请执行以下操作:field=userTextRec.split('\n')[0]。split(“,”)

谢谢,伙计。你能解释一下len,f,==0吗?Just tryna learn:)@GabeSaban
len()
是一个内置函数,用于计算列表中的元素数,
f”“
是python3.6+中的一个功能,名为f string:formatted string,==0,意思是“值等于0吗?”,在开始编程之前,你真的应该理解这些基本函数/关键字。我正在努力学习,除此之外,没有其他学习方法,我不会在尝试之前学习所有python命令,对吗?你完全正确,但是像
if
len
之类的东西应该是你首先学习的东西是的,我知道if,但是sql是我不喜欢的东西