如何防止同一数据库的多个条目?sqlite3现在不会执行了吗?

2024-06-16 16:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

目前,我已经编写了一些代码,允许用户输入用户名和密码,然后将其详细信息与数据库中的信息进行比较。它以前工作过,但现在出现了一个错误:

AttributeError: 'builtin_function_or_method' object has no attribute 'execute'

为什么现在不执行

另外,我对sqlite3还很陌生,我想知道如果值不在db中,如何只将它们插入db中,因为当前必须将值放到表中,以防止每次有人错误登录时重复添加它们。因为我不希望数据库充满相同的值


Tags: textinimportselfexecutedefusernamelogin
2条回答

您需要调用cursor方法

# ...
def GetDatabase(username,password):
    conn=sqlite3.connect('TEST.db')
    c=conn.cursor() # Call this one!
    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)
#...

我想你想要c = conn.cursor()而不是c = conn.cursorGetDatabase

至于重复项,在设计表时,可以向一列或一组列添加UNIQUE约束。这将自动拒绝数据库中的重复项

相关问题 更多 >