简体   繁体   English

如何在 Python 中使用 SQLITE3 进行登录?

[英]How do I make a login using SQLITE3 in Python?

ay yo this is my 1st post don't bully me... so im tryna make a login things using SQLITE3 in python (the module) and im trash (also if there is a better database module then can u tell me in I could use MySQL but I dont need to store 103213021032103123123 data so) and im not sure what to do here the code:是的,这是我的第一篇文章,不要欺负我......所以我试着在 python(模块)中使用 SQLITE3 进行登录,我是垃圾(如果有更好的数据库模块,那么你能告诉我我可以使用 MySQL 但我不需要存储 103213021032103123123 数据所以我不知道在这里做什么代码:

conn = sqlite3.connect("pooptest123.db")
c = conn.cursor()


def create_table():
    c.execute("CREATE TABLE login(Username VARCHAR, Passwords VARCHAR)")
    conn.commit


def enter():
    Username = input("Create username: ")
    Password = input("Create password: ")
    c.execute(
        "INSERT INTO login (Username, Passwords) VALUES (?, ?)", (Username, Password,)
    )
    conn.commit()


create_table()
enter()


def read():
    sql = 'SELECT * FROM login WHERE "Username" == Username'
    sql2 = "SELECT * FROM login WHERE 'Passwords' == Password"
    c.execute(sql, sql2)
    conn.commit


read()


def loginlol():
    sure = input("Login?(Y OR N: ")
    if sure == "y" or sure == "Y":
        b = input("Username: ")
        ca = input("Password: ")
        if b == sql:
            print("Username correct!")
        if c == sql2:

            print("Password correct!")
            print("Logging in...")


loginlol()

conn.close()

and it says this:它说:

File "C:\Users\dontlookhere\AppData\Local\Programs\Python\Python38-32\login.py", line 20, in <module>
    read()
  File "C:\Users\dontlookhere\AppData\Local\Programs\Python\Python38-32\login.py", line 18, in read
    c.execute(sql, sql2)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 49 supplied.

sry im dumb so and trash with oop and idk what im doing ok anyways thx对不起,我很笨,所以用 oop 和 idk 搞砸了,反正我在做什么 thx

Your first (but not last) mistake is你的第一个(但不是最后一个)错误是

 c.execute(sql, sql2)

execute() can't run two queries. execute()不能运行两个查询。 It runs sql as query but text in sql2 it uses as list of arguments for sql .它运行sql作为查询,但sql2中的文本用作 sql 的sql列表。

See your other execute :查看您的其他execute

c.execute(
    "INSERT INTO login (Username, Passwords) VALUES (?, ?)", (Username, Password,)
)

you have only one query INSERT and list of arguments (Username, Password,)您只有一个查询INSERT和 arguments 列表(Username, Password,)


Other mistake is that you expect that SELECT will assign result to variables sql and sql2 but it doesn't work this way.另一个错误是您希望SELECT将结果分配给变量sqlsql2但它不能以这种方式工作。 execute uses query sql but after that you have to use c.fetchall() or c.fetchone() to get result from database. execute使用查询sql但之后您必须使用c.fetchall()c.fetchone()从数据库中获取结果。

Simply you have to get some tutorial for databases in Python and learn all from scratch.只需在 Python 中获得一些数据库教程,然后从头开始学习。


EDIT:编辑:

Other big mistake is that you use two SELECT另一个大错误是你使用了两个 SELECT

'SELECT * FROM login WHERE "Username" == Username'
"SELECT * FROM login WHERE 'Passwords' == Password"

but this way you check username without password or password without username but you don't check password and username as pair.但是这样你检查没有passwordusername或没有usernamepassword ,但你不检查passwordusername成对。

Someone may use your useraname and my password and it will give True but it should gives True only for pair your username and your password or for pair my username and my password .有人可能会使用your useranamemy password ,它会给出True但它应该给出True仅用于配对your usernameyour password或配对my usernamemy password You should check it in one query using您应该在一个查询中使用

 WHERE Username == "?" AND Passwords == "?"

like喜欢

c.execute('SELECT * FROM login WHERE Username == "?" AND Passwords == "?"', (username, password)`

But there is other problem - you shouldn't keep password in plain text but you should keep it encrypted.但是还有其他问题 - 您不应该将密码保存为纯文本,但应该对其进行加密。 But it is problem for different question.但这是不同问题的问题。


More or less:或多或少:

def read(username, password):

    sql = 'SELECT * FROM login WHERE Username == "?" AND Passwords == "?"'
    c.execute(sql, (userame, password)
    result = c.fetchone()

    return result

EDIT:编辑:

I see another problem - in your database you may have the same username many times - you should make it UNIQUE我看到另一个问题-在您的数据库中,您可能多次使用相同的username -您应该将其UNIQUE


EDIT:编辑:

Example code.示例代码。

For test at start it drops table and create new one but normally you should create table only once.对于开始测试,它会删除表并创建新表,但通常您应该只创建一次表。

I add UNIQUE so it will raise error when you try to create second user with the same name.我添加了UNIQUE ,因此当您尝试创建具有相同名称的第二个用户时会引发错误。

It still need to encrypt password它仍然需要加密password

If username and password is correct then check() returns single row with user data.如果usernamepassword正确,则check()返回包含用户数据的单行。 If username or password is wrong then it returns None .如果usernamepassword错误,则返回None

Eventually in check() you can create query which use only Username =?最终在check()中,您可以创建仅使用Username =?的查询to get user data and later it checks password in this data without usign second sql .获取用户数据,稍后它会检查此数据中的password ,而无需使用第二个sql This way you may get user data even if he put wrong password .这样,即使他输入了错误的password ,您也可以获得用户数据。

import sqlite3


def create_table():
    query = "DROP TABLE IF EXISTS login"
    cursor.execute(query)
    conn.commit()
    
    query = "CREATE TABLE login(Username VARCHAR UNIQUE, Password VARCHAR)"
    cursor.execute(query)
    conn.commit()

def enter(username, password):
    query = "INSERT INTO login (Username, Password) VALUES (?, ?)"
    cursor.execute(query, (username, password))
    conn.commit()

def check(username, password):
    query = 'SELECT * FROM login WHERE Username = ? AND Password = ?'
    cursor.execute(query, (username, password))
    result = cursor.fetchone()
    conn.commit()
    print('[DEBUG][check] result:', result)
    return result

def loginlol():
    answer = input("Login (Y/N): ")

    if answer.lower() == "y":
        username = input("Username: ")
        password = input("Password: ")
        if check(username, password):
            print("Username correct!")
            print("Password correct!")
            print("Logging in...")
        else:
            print("Something wrong")

# --- main ---

conn = sqlite3.connect("pooptest123.db")
cursor = conn.cursor()

create_table()

Username = input("Create username: ")
Password = input("Create password: ")

enter(Username, Password)

#check(Username, Password)

loginlol()

cursor.close()
conn.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM