Create a database console application to perform the following applications.
a) Define a dictionary to store details for library books consisting of book id or title author name, year of publication and publisher.
b) Show how the information can be stored in a SQLite or MySQL database named books, retrieved by book id or author name, update using any book id or title and deleted using book id or title.
c) Handle all exceptions and validate data entry.
(a)
class BookFormat(Enum):
HARDCOVER, PAPERBACK, AUDIO_BOOK, EBOOK, NEWSPAPER, MAGAZINE, JOURNAL = 1, 2, 3, 4, 5, 6, 7
class BookStatus(Enum):
AVAILABLE, RESERVED, LOANED, LOST = 1, 2, 3, 4
class ReservationStatus(Enum):
WAITING, PENDING, CANCELED, NONE = 1, 2, 3, 4
class AccountStatus(Enum):
ACTIVE, CLOSED, CANCELED, BLACKLISTED, NONE = 1, 2, 3, 4, 5
class Address:
def __init__(self, street, city, state, zip_code, country):
self.__street_address = street
self.__city = city
self.__state = state
self.__zip_code = zip_code
self.__country = country
class Person(ABC):
def __init__(self, name, address, email, phone):
self.__name = name
self.__address = address
self.__email = email
self.__phone = phone
class Constants:
self.MAX_BOOKS_ISSUED_TO_A_USER = 5
self.MAX_LENDING_DAYS = 10
(b) . Importing the necessary modules
from tkinter import *
from PIL import ImageTk,Image
from tkinter import messagebox
import pymysql
Connection to MySql server
mypass = "root"
mydatabase="db"
con = pymysql.connect( host="localhost",user="root",password=mypass,database=mydatabase)
cur = con.cursor()
# Enter Table Names here
bookTable = "books"
. Function – View()
def View():
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#12a4d9")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
headingLabel = Label(headingFrame1, text="View Books", bg='black', fg='white', font = ('Courier',15))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
y = 0.25
Label(labelFrame, text="%-10s%-40s%-30s%-20s"%('BID','Title','Author','Status'),
bg='black',fg='white').place(relx=0.07,rely=0.1)
Label(labelFrame, text = "----------------------------------------------------------------------------",bg='black',fg='white').place (relx=0.05,rely=0.2)
getBooks = "select * from "+bookTable
try:
cur.execute(getBooks)
con.commit()
for i in cur:
Label(labelFrame,text="%-10s%-30s%-30s%-20s"%(i[0],i[1],i[2],i[3]) ,bg='black', fg='white').place(relx=0.07,rely=y)
y += 0.1
except:
messagebox.showinfo("Failed to fetch files from database")
quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)
quitBtn.place(relx=0.4,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
def deleteBook():
bid = bookInfo1.get()
deleteSql = "delete from "+bookTable+" where bid = '"+bid+"'"
deleteIssue = "delete from "+issueTable+" where bid = '"+bid+"'"
try:
cur.execute(deleteSql)
con.commit()
cur.execute(deleteIssue)
con.commit()
messagebox.showinfo('Success',"Book Record Deleted Successfully")
except:
messagebox.showinfo("Please check Book ID")
print(bid)
bookInfo1.delete(0, END)
root.destroy()
Function – delete()
def delete():
global bookInfo1,bookInfo2,bookInfo3,bookInfo4,Canvas1,con,cur,bookTable,root
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#006B38")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
headingLabel = Label(headingFrame1, text="Delete Book", bg='black', fg='white', font=('Courier',15))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
# Book ID to Delete
lb2 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')
lb2.place(relx=0.05,rely=0.5)
bookInfo1 = Entry(labelFrame)
bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)
#Submit Button
SubmitBtn = Button(root,text="SUBMIT",bg='#d1ccc0', fg='black',command=deleteBook)
SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
def issue():
global issueBtn,labelFrame,lb1,inf1,inf2,quitBtn,root,Canvas1,status
bid = inf1.get()
issueto = inf2.get()
issueBtn.destroy()
labelFrame.destroy()
lb1.destroy()
inf1.destroy()
inf2.destroy()
extractBid = "select bid from "+bookTable
try:
cur.execute(extractBid)
con.commit()
for i in cur:
allBid.append(i[0])
if bid in allBid:
checkAvail = "select status from "+bookTable+" where bid = '"+bid+"'"
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'avail':
status = True
else:
status = False
else:
messagebox.showinfo("Error","Book ID not present")
except:
messagebox.showinfo("Error","Can't fetch Book IDs")
issueSql = "insert into "+issueTable+" values ('"+bid+"','"+issueto+"')"
show = "select * from "+issueTable
updateStatus = "update "+bookTable+" set status = 'issued' where bid = '"+bid+"'"
try:
if bid in allBid and status == True:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
messagebox.showinfo('Success',"Book Issued Successfully")
root.destroy()
else:
allBid.clear()
messagebox.showinfo('Message',"Book Already Issued")
root.destroy()
return
except:
messagebox.showinfo("Search Error","The value entered is wrong, Try again")
print(bid)
print(issueto)
allBid.clear()
def returnn():
global SubmitBtn,labelFrame,lb1,bookInfo1,quitBtn,root,Canvas1,status
bid = bookInfo1.get()
extractBid = "select bid from "+issueTable
try:
cur.execute(extractBid)
con.commit()
for i in cur:
allBid.append(i[0])
if bid in allBid:
checkAvail = "select status from "+bookTable+" where bid = '"+bid+"'"
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'issued':
status = True
else:
status = False
else:
messagebox.showinfo("Error","Book ID not present")
except:
messagebox.showinfo("Error","Can't fetch Book IDs")
issueSql = "delete from "+issueTable+" where bid = '"+bid+"'"
print(bid in allBid)
print(status)
updateStatus = "update "+bookTable+" set status = 'avail' where bid = '"+bid+"'"
try:
if bid in allBid and status == True:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
messagebox.showinfo('Success',"Book Returned Successfully")
else:
allBid.clear()
messagebox.showinfo('Message',"Please check the book ID")
root.destroy()
return
except:
messagebox.showinfo("Search Error","The value entered is wrong, Try again")
allBid.clear()
root.destroy()
def returnn():
global SubmitBtn,labelFrame,lb1,bookInfo1,quitBtn,root,Canvas1,status
bid = bookInfo1.get()
extractBid = "select bid from "+issueTable
try:
cur.execute(extractBid)
con.commit()
for i in cur:
allBid.append(i[0])
if bid in allBid:
checkAvail = "select status from "+bookTable+" where bid = '"+bid+"'"
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'issued':
status = True
else:
status = False
else:
messagebox.showinfo("Error","Book ID not present")
except:
messagebox.showinfo("Error","Can't fetch Book IDs")
issueSql = "delete from "+issueTable+" where bid = '"+bid+"'"
print(bid in allBid)
print(status)
updateStatus = "update "+bookTable+" set status = 'avail' where bid = '"+bid+"'"
try:
if bid in allBid and status == True:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
messagebox.showinfo('Success',"Book Returned Successfully")
else:
allBid.clear()
messagebox.showinfo('Message',"Please check the book ID")
root.destroy()
return
except:
messagebox.showinfo("Search Error","The value entered is wrong, Try again")
allBid.clear()
root.destroy()
def returnn():
global SubmitBtn,labelFrame,lb1,bookInfo1,quitBtn,root,Canvas1,status
bid = bookInfo1.get()
extractBid = "select bid from "+issueTable
try:
cur.execute(extractBid)
con.commit()
for i in cur:
allBid.append(i[0])
if bid in allBid:
checkAvail = "select status from "+bookTable+" where bid = '"+bid+"'"
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'issued':
status = True
else:
status = False
else:
messagebox.showinfo("Error","Book ID not present")
except:
messagebox.showinfo("Error","Can't fetch Book IDs")
issueSql = "delete from "+issueTable+" where bid = '"+bid+"'"
print(bid in allBid)
print(status)
updateStatus = "update "+bookTable+" set status = 'avail' where bid = '"+bid+"'"
try:
if bid in allBid and status == True:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
messagebox.showinfo('Success',"Book Returned Successfully")
else:
allBid.clear()
messagebox.showinfo('Message',"Please check the book ID")
root.destroy()
return
except:
messagebox.showinfo("Search Error","The value entered is wrong, Try again")
allBid.clear()
root.destroy()
Function – returnBook()
def returnBook():
global bookInfo1,SubmitBtn,quitBtn,Canvas1,con,cur,root,labelFrame, lb1
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#006B38")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
headingLabel = Label(headingFrame1, text="Return Book", bg='black', fg='white', font=('Courier',15))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
# Book ID to Delete
lb1 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')
lb1.place(relx=0.05,rely=0.5)
bookInfo1 = Entry(labelFrame)
bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)
#Submit Button
SubmitBtn = Button(root,text="Return",bg='#d1ccc0', fg='black',command=returnn)
SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Comments
Leave a comment