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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
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() |