Please help me with my code.
Here is my code. And below this code, the database file is attached.
Here is my code. And below this code, the database file is attached.
import sqlite3
from tkinter import * #importing tkinter for GUI creation
from tkinter import messagebox
con = sqlite3.connect('question_bank')
def search(): #method for searching database bodules
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="select * from modulename where moduleno=?",moduleno.get()
cur.execute(sql)
result=cur.fetchone()
name.set(result[1])
"""el.configure(state='disabled')"""
con.close()
except:
messagebox.showinfo('No Data', 'No such data available...')
finally:
clear()
def clear():
moduleno.set('')
name.set('')
"""el.configure(state='normal')"""
def add(): #method for adding into database bodules
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="insert into modulename values(?, ?)",(moduleno.get(), name.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record saved...')
except:
messagebox.showinfo('Error', 'Error in data entry...')
finally:
clear()
def update(): #method for updating database bodules
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="update modulename set name=? where moduleno=?",(name.get(), moduleno.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record updated...')
except:
messagebox.showinfo('Error', 'Error occured...')
finally:
clear()
def delete(): #method for deleting database bodules
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="delete from modulename where moduleno=?",(moduleno.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record deleted...')
except:
messagebox.showinfo('Error', 'Error occured...')
finally:
clear()
w1=Tk()
w1.title('For Administration')
w1.geometry('500x550')
ptitle=Label(w1, text= '''Add, delete and modify the records
from the modules table''')
ptitle.grid(row=0, column=0, columnspan=2)
moduleno=StringVar()
name=StringVar()
l1=Label(w1, text = 'ModuleNo')
e1=Entry(w1, textvariable=moduleno)
l2=Label(w1, text = 'Name')
e2=Entry(w1, textvariable=name)
#buttons for modules updating
b1=Button(w1, text = 'Search', command=search)
b2=Button(w1, text = 'Add', command=add)
b3=Button(w1, text = 'Update', command=update)
b4=Button(w1, text = 'Delete', command=delete)
b5=Button(w1, text = 'Clear', command=clear)
l1.grid(row=1, column=0)
e1.grid(row=1, column=1)
b1.grid(row=1, column=2)
l2.grid(row=2, column=0)
e2.grid(row=2, column=1)
b2.grid(row=3, column=0)
b3.grid(row=3, column=1)
b4.grid(row=4, column=0)
b5.grid(row=4, column=1)
#Accessing and editing questions table in our question_bank database
def search(): #method for searching database questions
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="select * from questions where questionno=?",questionno.get()
cur.execute(sql)
result=cur.fetchone()
text.set(result[1])
"""el.configure(state='disabled')"""
con.close()
except:
messagebox.showinfo('No Data', 'No such data available...')
clear()
def clear():
questionno.set('')
text.set('')
"""el.configure(state='normal')"""
def add(): #method for adding into database questions
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="insert into questions values(?, ?)",(questionno.get(), text.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record saved...')
except:
messagebox.showinfo('Error', 'Error in data entry...')
finally:
clear()
def update(): #method for updating database questions
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="update questions set text=? where questionno=?",(text.get(), questionno.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record updated...')
except:
messagebox.showinfo('Error', 'Error occured...')
finally:
clear()
def delete(): #method for deleting database questions
try:
con = sqlite3.connect('question_bank')
cur=con.cursor()
sql="delete from questions where questionno=?",(questionno.get())
cur.execute(sql)
con.commit()
con.close()
messagebox.showinfo('Success', 'Record deleted...')
except:
messagebox.showinfo('Error', 'Error occured...')
finally:
clear()
ptitle=Label(w1, text= '''Add, delete and modify the questions
from the modules''')
ptitle.grid(row=5, column=0, columnspan=2)
questionno=StringVar()
text=StringVar()
l1=Label(w1, text = 'QuestionNo')
e1=Entry(w1, textvariable=questionno)
l2=Label(w1, text = 'Text')
e2=Entry(w1, textvariable=text)
#buttons for questions database editing
b6=Button(w1, text = 'Search', command=search)
b7=Button(w1, text = 'Add', command=add)
b8=Button(w1, text = 'Update', command=update)
b9=Button(w1, text = 'Delete', command=delete)
b10=Button(w1, text = 'Clear', command=clear)
l1.grid(row=6, column=0)
e1.grid(row=6, column=1)
b6.grid(row=6, column=2)
l2.grid(row=7, column=0)
e2.grid(row=7, column=1)
b7.grid(row=8, column=0)
b8.grid(row=8, column=1)
b9.grid(row=9, column=0)
b10.grid(row=9, column=1)
w1.mainloop()
