0

これは、生徒の記録を追加、削除、編集、および表示する単純な python プログラムです。問題は、DELETE と EDIT が機能しないことです。理由はわかりませんが、「?」を削除しようとしたとき。DELETE ダンクションで、列が 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="\n\tStudent Number"+"\t\tName"+"\t\tAge"+"\t\tBirthday"+"\t\tAddress"+"\t\tEmail"+"\t\tCourse"+"\t\tYear"+"\t\nSection")
    lbl1.pack()
    for row in cur.execute("SELECT * FROM student"):
        lbl2=tkinter.Label(win,width=300,text= row[0] + '\t\t' + row[1] + '\t' + row[2] + '\t\t' + row[3] + '\t\t' + row[4] + '\t\t' + row[5] + '\t\t' + row[6] + '\t\t' + row[7] + '\t\t' + 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()
4

2 に答える 2

0

116 行目と 117 行目でemail、テーブルに挿入しようとしたときに 2 回リストしました。

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いつかまた問題が発生する場合は、発生しているエラーを投稿してください。

于 2013-11-08T09:19:23.383 に答える
0
INSERT INTO student(studentnum,name,age,email,birthday,address,email,course,year,section)
                                          ^                      ^

emailが繰り返されます。

注: DELETE ステートメントでもパラメーターを使用する必要があります。

cur.execute("DELETE FROM student WHERE name = ?", (studentnum1))
于 2013-11-08T09:20:08.070 に答える