Python Code
Python Code
Python Code
class DB: #creating a class DB with functions to perform various operations on the
database.
self.cur.execute(
"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title TEXT, author TEXT, isbn
TEXT)") #creating a table called book with id, title, author and isbn as columns.
def view(self): #To view all the rows present in the table
self.cur.execute("SELECT * FROM book") #Execute function is to perform the SQL operations. Here,
it produces all the rows from the table.
rows = self.cur.fetchall() #fetching all the rows one by one from the table and storing it in list rows
return rows
def insert(self, title, author, isbn): #inserting a new row in the table.
self.cur.execute("INSERT INTO book VALUES (NULL,?,?,?)", (title, author, isbn,)) #passing values to
the function to store them in the columns
self.conn.commit()
self.view()
def update(self, id, title, author): #to update the values of the selected row with the values passed
by the user
self.cur.execute("UPDATE book SET title=?, author=? WHERE id=?", (title, author, id,))
self.conn.commit()
self.view()
def delete(self, id): #to delete the row from the table given the value of the id of the
selected row.
self.conn.commit()
self.view()
def search(self, title="", author=""): #to search for a given entry in the table given either the value of
the title or author name
rows = self.cur.fetchall()
return rows
db = DB() #created an object of the class DB. Now database is connected and a new table book has been
formed.
selected_tuple = list1.get(index)
e1.delete(0, END) #deleting the value so that can be used again for next book
e2.delete(0, END)
e3.delete(0, END)
def view_command(): #to print all the rows of the table using view function of the class DB on to
the screen
for row in db.view(): #loop until we reach the end of the table book
def search_command(): #to print the row we want based on title or author
for row in db.search(title_text.get(), author_text.get()): #get the name of the title or the author and
pass it to the search function of class DB
list1.insert(END, row) #will insert all the rows having the same value of title or author
list1.insert(END, (title_text.get(), author_text.get(), isbn_text.get())) #insert into the list and then the
table, the values given by the user
db.delete(selected_tuple[0]) #calls the delete function of the class DB and passes the id as the
parameter and condition
def update_command():
dd = db
if messagebox.askokcancel("Quit", "Do you want to quit?"): #when ok is clicked, displays the following
message
window.destroy()
l1.grid(row=0, column=0) #determining size of the input grid for these labels
l2 = Label(window, text="Author")
l2.grid(row=0, column=2)
l3 = Label(window, text="ISBN")
l3.grid(row=1, column=0)
title_text = StringVar()
e1 = Entry(window, textvariable=title_text) #taking input from the user in the grid and storing it in a
string variable
e1.grid(row=0, column=1)
e2 = Entry(window, textvariable=author_text)
e2.grid(row=0, column=3)
e3 = Entry(window, textvariable=isbn_text)
e3.grid(row=1, column=1)
list1 = Listbox(window, height=25, width=65) #creating the list space to display all the rows of the table
sb1 = Scrollbar(window) #creating a scrollbar for the window to scroll through the list entries
list1.configure(yscrollcommand=sb1.set) #configuring the scroll function for the scrollbar object sb1
sb1.configure(command=list1.yview)
list1.bind('<<ListboxSelect>>', get_selected_row)
b2.grid(row=3, column=3)
b3.grid(row=4, column=3)
b6.grid(row=7, column=3)
window.mainloop() #carry the functioning of the GUI window on a loop until it is closed using the
destructor