如何防止同一个db的多个条目?和sqlite3现在不会执行?

时间:2017-09-17 16:01:25

标签: python database sqlite

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充满相同的值。

2 个答案:

答案 0 :(得分:2)

我认为您需要c = conn.cursor()而不是c = conn.cursor GetDatabase

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

答案 1 :(得分:1)

您需要调用光标方法。

# ...
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)
#...