from tkinter import *
import tkinter.messagebox as tm
import sqlite3
import array
import pygame
from pygame import *
import random
pygame.init()
f=pygame.font.SysFont(None,60)
class Login:
def __init__(self):
Label1 = Label(root,text = "Username")
Label2 = Label(root,text = "Password")
self.Entry1 = Entry(root)
self.Entry2 = Entry(root,show = "*")
Label1.grid(row=0)#default column is 0
Label2.grid(row=1)
self.Entry1.grid(row = 0,column = 1)
self.Entry2.grid(row = 1,column = 1)
root.minsize(width = 300,height = 80)
lgbutton = Button(text = "Login",command = self.ButtonClicked)
lgbutton.grid(columnspan = 2)
def ButtonClicked(self):
username = self.Entry1.get()
password = self.Entry2.get()
GetDatabase(username,password)#compares the username and password to the ones stored in the database
def GetDatabase(username,password):
conn=sqlite3.connect('TEST.db')
c=conn.cursor
c.execute("CREATE TABLE IF NOT EXISTS StudentLogin(surname TEXT, username TEXT, password INT);")
c.execute("CREATE TABLE IF NOT EXISTS TeacherLogin(surname TEXT, username TEXT, password INT,class TEXT);")
c.execute('''INSERT INTO StudentLogin (surname,username,password)
VALUES
('Fred', 'Smith',123),
('John', 'Tucker',123),
('Michael', 'Jefford',123),
('Robert', 'Thomas',123);''')
c.execute('''INSERT INTO TeacherLogin(surname,username,password,class)
VALUES('Tucker','HTucker',123,'12C/C1'),
('Cooper','ACooper',142,'12C/Ma1'),
('Smith','KSmith',1223,'12C/Ch1'),
('Elston','FElston',454,'12C/Ch1');''')
StudentUsername=[]
StudentPassword=[]
TeacherUsername=[]
TeacherPassword=[]
c.execute("SELECT * FROM TeacherLogin;")
for column in c:
TeacherUsername.append(column[1])
TeacherPassword.append(column[2])
print(TeacherUsername)
print(TeacherPassword)
c.execute("SELECT * FROM StudentLogin;")
for column in c:
StudentUsername.append(column[1])
StudentPassword.append(column[2])
print(StudentUsername)
print(StudentPassword)
c.execute("DROP TABLE TeacherLogin;")
c.execute("DROP TABLE StudentLogin;")
if username in StudentUsername:
correct=int(StudentUsername.index(username))
if int(password) == StudentPassword[correct]:
#if password in Password :
tm.showinfo("Login info", "Welcome "+username)
root.destroy()
Simulation()
elif username in TeacherUsername:
correct=int(TeacherUsername.index(username))
if int(password)==TeacherPassword[correct]:
tm.showinfo("Login info", "Welcome "+username)
root.destroy()
TeacherLogin()
else:
tm.showerror("Login error", "Incorrect username or password")
conn.commit()
conn.close()
def TeacherLogin()
pass
def StudentLogin():
pass
root=Tk() #creating blank window
root.resizable(width=False,height=False)
root.title("Please login.")
root.minsize(width=300,height=80)
Login()
root.mainloop()
在我已经写了一些代码,允许用户输入自己的用户名和密码,然后将自己的详细对比,以在数据库中的那些时刻。它以前工作过,但现在出现的错误:如何防止同一个分贝的多个条目?和sqlite3现在不会执行?
AttributeError: 'builtin_function_or_method' object has no attribute 'execute'
它为什么现在不执行?
另外,我很新的sqlite3,我想知道如何将值插入数据库,如果他们不存在,因为目前它必须删除表中的值,以阻止他们从每次有人登录不正确时都会重复添加。因为我不想让数据库充满相同的值。
我该怎么做?如果数据已经在数据库中,那么将如何检查数据? –
当你创建一个表时,你可以指定一个字段(列)被限制为独特的值,如'CREATE TABLE t1(a,b UNIQUE);'。如果你想要对现有表进行约束,那么使用SQLite会更棘手。这两个文件可以在这里找到。 https://www.techonthenet.com/sqlite/unique.php – 0x11