数据库中的唯一约束失败

时间:2019-05-23 15:08:04

标签: python database sqlite tkinter

我使用数据库浏览器创建了一个数据库,因为手动创建数据库并没有那么顺利。正如其他开发人员所建议的那样,我什至创建了一个全局变量,但这之后又是另一个障碍。请帮我,因为我只是15岁的新手。 附:不要对我的愚蠢问题生气。 我不了解这个例外。

from tkinter import*
import sqlite3

conn = sqlite3.connect("database.db")

def stud_win():



    studwin=Toplevel(root)
    studwin.title('Student registration')
    studwin.resizable(0,0)
    a = Label(studwin ,text = "ID" ).grid(row = 0,column = 0)
    b = Label(studwin ,text = "Date").grid(row = 1,column = 0)
    c = Label(studwin ,text = "Address").grid(row = 2,column = 0)
    d = Label(studwin ,text = "Mobile Number").grid(row = 3,column = 0)
    e = Label(studwin, text = "Name").grid(row=0, column=2)
    f = Label(studwin, text = "Qualification").grid(row=1, column=2)
    g = Label(studwin ,text = "Email").grid(row = 2,column = 2)
    h = Label(studwin, text = "Gender").grid(row = 3, column= 2)
    ID=IntVar()
    a1 = Entry(studwin, textvariable=ID).grid(row = 0,column = 1)
    Date = StringVar()
    b1 = Entry(studwin, textvariable=Date).grid(row = 1,column = 1)
    Address = StringVar()
    c1 = Entry(studwin,textvariable=Address).grid(row = 2,column = 1)
    Number = StringVar()
    d1 = Entry(studwin,textvariable=Number).grid(row = 3,column = 1)
    Name = StringVar()
    e1 = Entry(studwin,textvariable=Name).grid(row = 0,column = 3)
    Qualification = StringVar()
    f1 = Entry(studwin,textvariable=Qualification).grid(row = 1,column = 3)
    Email = StringVar()
    g1 = Entry(studwin,textvariable=Email).grid(row = 2,column = 3)
    gender = StringVar()
    R1 = Radiobutton(studwin, text="Male", value="male", var=gender)
    R2 = Radiobutton(studwin, text="Female", value="female", var=gender)
    R1.grid(row = 3,column = 3)
    R2.grid(row = 3,column = 4)
    id1 = ID.get()
    date1 = Date.get()
    address1 = Address.get()
    number1 = Number.get()
    name1 = Name.get()
    qualification1 = Qualification.get()
    email1 = Email.get()
    gender1= gender.get()





    def btn_click1():

        conn = sqlite3.connect('database.db')

        cu=conn.cursor()
        cu.execute('INSERT INTO Student (ID,Date,Address,Number ,Name,Qualification,Email,Gender) VALUES(?,?,?,?,?,?,?,?)',
                       (id1, date1, address1, number1, name1,qualification1,email1,gender1,))
        conn.commit()

    Button(studwin, text="submit", command=btn_click1).grid(row=4, columnspan=4)
    studwin.mainloop()









root=Tk()
root.title("Registration form")
root.resizable(0,0)
menubar=Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Student", command=stud_win)
filemenu.add_command(label="Employee")
menubar.add_cascade(label="Register", menu=filemenu)
form_menu = Menu(menubar, tearoff=0)
form_menu.add_command(label="Enrollment form")
menubar.add_cascade(label="Transaction", menu=form_menu)
exitmenu=Menu(menubar, tearoff=0)
exitmenu.add_command(label="Exit App", command=root.quit)
menubar.add_cascade(label="Exit", menu=exitmenu)



root.config(menu=menubar)
root.mainloop()



Exception in Tkinter callback
Traceback (most recent call last):
  File "F:\Python3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "F:/Python Projects/Registration form.py", line 59, in btn_click1
    (id1, date1, address1, number1, name1,qualification1,email1,gender1,))
sqlite3.IntegrityError: UNIQUE constraint failed: Student.ID

0 个答案:

没有答案