SQLite3从数据库中读取用户名和密码

时间:2016-03-08 18:57:45

标签: python database tkinter sqlite

我正在尝试使用数据库从中读取有效的登录信息。一列是用户名,另一列是密码。我如何编写我的代码,以便 IF 来自用户的用户名条目和用户的密码条目匹配数据库中的一行,它将验证登录并移动到下一页,这是示例名为" Screen"

我在下面制作了一个演示登录页面,其中添加了一个创建数据库功能。我必须编辑/添加什么来填补我的目标。

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()

感谢您花时间阅读并感谢任何帮助

0 个答案:

没有答案