DELETE and EDIT is not working in my python program

Posted by user2968025 on Stack Overflow See other posts from Stack Overflow or by user2968025
Published on 2013-11-08T08:19:03Z Indexed on 2013/11/09 21:54 UTC
Read the original article Hit count: 554

Filed under:
|
|
|
|

This is a simple python program that ADD, DELETE, EDIT and VIEW student records. The problem is, DELETE and EDIT is not working. I dont know why but when I tried removing one '?' in the DELETE dunction, I had the error that says there are only 8 columns and it needs 10. But originally, there are only 9 columns. I don't know where it got the other one to make it 10. Please help.. :(

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

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlite