DELETE and EDIT在我的python程序中不起作用



这是一个简单的python程序,可以添加,删除,编辑和查看学生记录。问题是,删除和编辑不起作用。我不知道为什么,但是当我尝试删除 DELETE dunction 中的一个"?"时,我遇到了错误,说只有 8 列,需要 10 列。但最初,只有 9 列。我不知道它从哪里弄来的另一个让它变成 10。请帮忙..:(

import sys
import sqlite3
import tkinter
import tkinter as tk
from tkinter import *
from tkinter.ttk import *

def newRecord():
    studentnum=""
    name=""
    age=""
    birthday=""
    address=""
    email=""
    course=""
    year=""
    section=""
    con=sqlite3.connect("Students.db")
    cur=con.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS student(studentnum TEXT, name TEXT, age TEXT, birthday TEXT, address TEXT, email TEXT, course TEXT, year TEXT, section TEXT)")
    def save():
        studentnum=en1.get()
        name=en2.get()
        age=en3.get()
        birthday=en4.get()
        address=en5.get()
        email=en6.get()
        course=en7.get()
        year=en8.get()
        section=en9.get()
        student=(studentnum,name,age,birthday,address,email,course,year,section)
        cur.execute("INSERT INTO student(studentnum,name,age,birthday,address,email,course,year,section) VALUES(?,?,?,?,?,?,?,?,?)",student)
        con.commit()
    win=tkinter.Tk();win.title("Students")
    lbl=tkinter.Label(win,background="#000",foreground="#ddd",width=30,text="Add Record")
    lbl.pack()
    lbl1=tkinter.Label(win,width=30,text="Student Number : ")
    lbl1.pack()
    en1=tkinter.Entry(win,width=30)
    en1.pack()
    lbl2=tkinter.Label(win,width=30,text="Name : ")
    lbl2.pack()
    en2=tkinter.Entry(win,width=30)
    en2.pack()
    lbl3=tkinter.Label(win,width=30,text="Age : ")
    lbl3.pack()
    en3=tkinter.Entry(win,width=30)
    en3.pack()
    lbl4=tkinter.Label(win,width=30,text="Birthday : ")
    lbl4.pack()
    en4=tkinter.Entry(win,width=30)
    en4.pack()
    lbl5=tkinter.Label(win,width=30,text="Address : ")
    lbl5.pack()
    en5=tkinter.Entry(win,width=30)
    en5.pack()
    lbl6=tkinter.Label(win,width=30,text="Email : ")
    lbl6.pack()
    en6=tkinter.Entry(win,width=30)
    en6.pack()
    lbl7=tkinter.Label(win,width=30,text="Course : ")
    lbl7.pack()
    en7=tkinter.Entry(win,width=30)
    en7.pack()
    lbl8=tkinter.Label(win,width=30,text="Year : ")
    lbl8.pack()
    en8=tkinter.Entry(win,width=30)
    en8.pack()
    lbl9=tkinter.Label(win,width=30,text="Section : ")
    lbl9.pack()
    en9=tkinter.Entry(win,width=30)
    en9.pack()
    btn1=tkinter.Button(win,background="#000",foreground="#ddd",width=30,text="Save Student",command=save)
    btn1.pack()
def editRecord():
    studentnum1=""
    def edit():
        studentnum1=en10.get()
        studentnum=""
        name=""
        age=""
        birthday=""
        address=""
        email=""
        course=""
        year=""
        section=""

        con=sqlite3.connect("Students.db")
        cur=con.cursor()
        row=cur.fetchone()
        cur.execute("DELETE FROM student WHERE name = '%s'" % studentnum1)
        con.commit()          
        def save():
            studentnum=en1.get()
            name=en2.get()
            age=en3.get()
            birthday=en4.get()
            address=en5.get()
            email=en6.get()
            course=en7.get()
            year=en8.get()
            section=en8.get()
            student=(studentnum,name,age,email,birthday,address,email,course,year,section)
            cur.execute("INSERT INTO student(studentnum,name,age,email,birthday,address,email,course,year,section) VALUES(?,?,?,?,?,?,?,?,?)",student)
            con.commit()
        win=tkinter.Tk();win.title("Students")
        lbl=tkinter.Label(win,background="#000",foreground="#ddd",width=30,text="Edit Reocrd :"+'t'+studentnum1)
        lbl.pack()
        lbl1=tkinter.Label(win,width=30,text="Student Number : ")
        lbl1.pack()
        en1=tkinter.Entry(win,width=30)
        en1.pack()
        lbl2=tkinter.Label(win,width=30,text="Name : ")
        lbl2.pack()
        en2=tkinter.Entry(win,width=30)
        en2.pack()
        lbl3=tkinter.Label(win,width=30,text="Age : ")
        lbl3.pack()
        en3=tkinter.Entry(win,width=30)
        en3.pack()
        lbl4=tkinter.Label(win,width=30,text="Birthday : ")
        lbl4.pack()
        en4=tkinter.Entry(win,width=30)
        en4.pack()
        lbl5=tkinter.Label(win,width=30,text="Address : ")
        lbl5.pack()
        en5=tkinter.Entry(win,width=30)
        en5.pack()
        lbl6=tkinter.Label(win,width=30,text="Email : ")
        lbl6.pack()
        en6=tkinter.Entry(win,width=30)
        en6.pack()
        lbl7=tkinter.Label(win,width=30,text="Course : ")
        lbl7.pack()
        en7=tkinter.Entry(win,width=30)
        en7.pack()
        lbl8=tkinter.Label(win,width=30,text="Year : ")
        lbl8.pack()
        en8=tkinter.Entry(win,width=30)
        en8.pack()
        lbl9=tkinter.Label(win,width=30,text="Section : ")
        lbl9.pack()
        en9=tkinter.Entry(win,width=30)
        en9.pack()
        btn1=tkinter.Button(win,background="#000",foreground="#ddd",width=30,text="Save Record",command=save)
        btn1.pack()
    win=tkinter.Tk();win.title("Edit Student")
    lbl=tkinter.Label(win,background="#000",foreground="#ddd",width=30,text="Edit Record")
    lbl.pack()
    lbl10=tkinter.Label(win,width=30,text="Student Number : ")
    lbl10.pack()
    en10=tkinter.Entry(win)
    en10.pack()
    btn2=tkinter.Button(win,background="#000",foreground="#ddd",width=30,text="Edit",command=edit)              
    btn2.pack()

def deleteRecord():
    studentnum1=""
    win=tkinter.Tk();win.title("Delete Student Record")
    lbl=tkinter.Label(win,background="#000",foreground="#ddd",width=30,text="Delete Record")
    lbl.pack()
    lbl10=tkinter.Label(win,text="Student Number")
    lbl10.pack()
    en10=tkinter.Entry(win)
    en10.pack()
    def delete():
        studentnum1=en10.get()
        con=sqlite3.connect("Students.db")
        cur=con.cursor()
        row=cur.fetchone()
        cur.execute("DELETE FROM student WHERE name = '%s';" % studentnum1)
        con.commit()
        win=tkinter.Tk();win.title("Record Deleted")
        lbl=tkinter.Label(win,background="#000",foreground="#ddd",width=30,text="Record Deleted :")
        lbl.pack()
        lbl=tkinter.Label(win,width=30,text=studentnum1)
        lbl.pack() 
        btn=tkinter.Button(win,background="#000",foreground="#ddd",width=30,text="Ok",command=win.destroy)
        btn.pack()
    btn2=tkinter.Button(win,background="#000",foreground="#ddd",width=30,text="Delete",command=delete)
    btn2.pack()
def viewRecord():
    con=sqlite3.connect("Students.db")
    cur=con.cursor()
    win=tkinter.Tk();win.title("View Student Record");
    row=cur.fetchall()
    lbl1=tkinter.Label(win,background="#000",foreground="#ddd",width=300,text="ntStudent Number"+"ttName"+"ttAge"+"ttBirthday"+"ttAddress"+"ttEmail"+"ttCourse"+"ttYear"+"tnSection")
    lbl1.pack()
    for row in cur.execute("SELECT * FROM student"):
        lbl2=tkinter.Label(win,width=300,text= row[0] + 'tt' + row[1] + 't' + row[2] + 'tt' + row[3] + 'tt' + row[4] + 'tt' + row[5] + 'tt' + row[6] + 'tt' + row[7] + 'tt' + row[8] + 'n')
        lbl2.pack()
    con.close()
    but1=tkinter.Button(win,background="#000",foreground="#fff", width=150,text="Close",command=win.destroy)
    but1.pack()
root=tkinter.Tk();root.title("Student Records")
menubar=tkinter.Menu(root)
manage=tkinter.Menu(menubar,tearoff=0)
manage.add_command(label='New Record',command=newRecord)
manage.add_command(label='Edit Record',command=editRecord)
manage.add_command(label='Delete Record',command=deleteRecord)
menubar.add_cascade(label='Manage',menu=manage)
view=tkinter.Menu(menubar,tearoff=0)
view.add_command(label='View Record',command=viewRecord)
menubar.add_cascade(label='View',menu=view)
root.config(menu=menubar)
lbl=tkinter.Label(root,background="#000",foreground="#ddd",font=("Verdana",15),width=30,text="Student Records")
lbl.pack()
lbl1=tkinter.Label(root,text="nSubmitted by :")
lbl1.pack()
lbl2=tkinter.Label(root,text="Chavez, Vissia Nicole P")
lbl2.pack()
lbl3=tkinter.Label(root,text="BSIT 4-4")
lbl3.pack()
INSERT INTO student(studentnum,name,age,email,birthday,address,email,course,year,section)
                                          ^                      ^

email重复。

注意:您还应该在 DELETE 语句中使用参数:

cur.execute("DELETE FROM student WHERE name = ?", (studentnum1))
在第

116 行和第 117 行,您在尝试插入表格时列出了两次email

student=(studentnum,name,age,email,birthday,address,email,course,year,section)
cur.execute("INSERT INTO student(studentnum,name,age,email,birthday,address,email,course,year,section) VALUES(?,?,?,?,?,?,?,?,?)",student)

应该是:

student=(studentnum,name,age,birthday,address,email,course,year,section)
cur.execute("INSERT INTO student(studentnum,name,age,birthday,address,email,course,year,section) VALUES(?,?,?,?,?,?,?,?,?)",student)

附带说明一下,研究 PEP8 可能没有什么坏处,它可以帮助您进行调试。

另外,我认为您的delete功能也无法正常工作。

注意:如果您在某个时候再次遇到问题,发布您收到的错误的traceback会很有帮助。

最新更新