简体   繁体   中英

SQLite3 reading Usernames&Passwords from database

I am attempting to use a database to read valid Logins from. One column is Username the other is Password. How do i write my code so that IF the Username entry from the user and the password entry from the user match a row within the database it will validate the log in and move onto the next page which is this example is called "Screen"

I have made a demo login page below in which i have added a creating database function. What must i edit/add to for fill what my aim is.

from tkinter import * #imports all the GUI libraries
from tkinter import messagebox #so it can be used outside idle
from tkinter import ttk
import sqlite3

class LoginPopup():
    #constructor sets up the buttons
    def __init__(Top):
        Top.GenericGui = Tk()#creating a window
        Top.GenericGui.title('Login')
        Top.GenericGui.geometry('300x200+250+30')
        Top.Check = 1
        Top.GenericGui.iconbitmap('home.ico')
        Password = StringVar()
        Username = StringVar()
        conn = sqlite3.connect('Logins.db')
        c = conn.cursor()

    #takes you back to the main menu
    def BackToMain():
        Top.delete()
        openscreen = Screen()


    def create_table():
        c.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(Username TEXT, Password TEXT)")

    def read_from_db():
        c.execute('SELECT * FROM stuffToPlot')
        data = c.fetchone()
        for row in data:
            print(row)

    def UsernameAndPassword(Top):
        UsernameAttempt = Username.get()#how to get value from entry box
        PasswordAttempt = Password.get()#how to get value from entry box

    #Login Components
    LoginVerify = ttk.Button(Top.GenericGui, text="Login", command = lambda: UsernameAndPassword(Top)).place(x=220,y=170)
    ttk.Entry(Top.GenericGui,textvariable = Username,width = 20).place(x=140,y=60)
    ttk.Entry(Top.GenericGui,show = '*',textvariable = Password,width = 20).place(x=140,y=100)#can hide letters entered with *
    ttk.Label(Top.GenericGui,text = 'Username:').place(x=40,y=60)
    ttk.Label(Top.GenericGui,text = 'Password:').place(x=40,y=100)
    Label(Top.GenericGui,text = '  Please Enter Your Username \nand Password ',font = ('TkDefaultFont',12)).pack(side = TOP, fill = X)


    create_table()    
    read_from_db()
    c.close
    conn.close()

class Screen(LoginPopup):

    #constructor sets up the buttons
    def __init__(Top):
        Top.GenericGui = Tk()#creating a window
        Top.GenericGui.title('Screen')
        Top.GenericGui.geometry('300x200+250+30')
        Top.Check = 1
        Top.GenericGui.iconbitmap('home.ico')
        Password = StringVar()
        Username = StringVar()

runprogram = LoginPopup()

Thanks for taking your time to read and any assistance would be appreciated

Write a generic SQL query to get all matching rows:

sql = "SELECT * FROM StuffToPlot WHERE Username = ? and Password = ?"

Then execute the query, filling in the specific values for username and password:

c.execute(sql, (UsernameAttempt, PasswordAttempt))

If you get no results from this query, then the username and/or password did not match.

If you get exactly one result, then they matched.

If you get more than one result, then something has gone terribly wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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