[英]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查询以获取所有匹配的行:
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. 如果得到不止一个结果,则说明出现了严重错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.