code
code
import mysql.connector as ms
def addbk():
while True:
try:
cur.execute()
db.commit()
print()
if check.lower()=='y':
pass
else:
break
print()
except:
print('An unexpected error occurred, please try again')
db.rollback()
print()
break
def change():
try:
cur.execute(change)
db.commit()
print()
except:
db.rollback()
print()
def delbook():
try:
cur.execute(d)
print()
except:
db.rollback()
print()
def search():
try:
1. BookId
2. Book name
if param==1:
cur.execute(sql3)
res3=cur.fetchall()
print()
elif param==2:
res4=cur.fetchall()
print(i)
print()
else:
print()
except:
print()
def viewallbooks():
try:
cur.execute(sel)
res=cur.fetchall()
for i in res:
print(i)
print()
except:
print()
def author():
try:
bkauth=input("Enter the author whose books you want to view: ")
cur.execute(sql2)
res2=cur.fetchall()
for i in res2:
print(i)
print()
except:
print()
def genre():
try:
cur.execute(sql1)
res1=cur.fetchall()
for i in res1:
print(i)
print()
except:
print()
#Adding issue records
def addrec():
while True:
try:
cur.execute(ch)
res=cur.fetchone()
if res[0]>0:
cur.execute(add)
db.commit()
cur.execute(reduce)
db.commit()
print()
else:
print()
pass
else:
break
print()
except:
db.rollback()
print()
def delissue():
try:
cur.execute(dell)
db.commit()
cur.execute(inc)
db.commit()
print()
except:
db.rollback()
print()
def viewallissues():
try:
cur.execute(show)
result=cur.fetchall()
for i in result:
print(i)
print()
except:
print()
def checkrecords():
try:
1. Issue ID
2. Name of issuer'')
if check==1:
for i in res:
print(i)
print()
elif check==2:
cur.execute(run)
res=cur.fetchall()
for i in res:
print(i)
print()
else:
print('invalid input')
print()
except:
print()
def findwho():
try:
cur.execute(find)
r=cur.fetchall()
for i in r:
print(i)
print()
except:
print()
def showselrec():
try:
cur.execute(show)
r=cur.fetchall()
for i in r:
print(i)
print()
except:
print()
def dateissue():
try:
cur.execute(show)
r=cur.fetchall()
for i in r:
print(i)
print()
except:
print()
def checkdelay():
try:
print("A book must be returned within 3 weeks of issuing else it'll have
to pay a return delay fine of 400 rupees")
print()
cur.execute(sql)
y=cur.fetchall()
date=str(y[0][0])
year=int(date[:4])
month=int(date[5:7])
day=int(date[8:])
leap=0
leap=1
elif year%400==0:
leap=1
else:
leap=0
days=0
if date[5:7] in ['01','03','05','07','08','10','12']:
monthdays=31
monthdays=30
elif date[5:7]=='02':
if leap==1:
monthdays=29
elif leap==0:
monthdays=28
else:
pass
#new date
if monthdays==31:
if day<11:
newday=day+21
if len(str(month))>1:
m=str(month)
else:
m='0'+str(month)
newdate=(str(year)+'-'+m+'-'+str(newday))
newyear=year+1
newmonth='01'
newdate=day-10
if len(str(newdate))>1:
newdate=(str(newyear)+'-'+newmonth+'-'+str(newdate))
else:
newdate=(str(newyear)+'-'+newmonth+'-'+'0'+str(newdate))
newday2=day-10
if len(str(newday2))>1:
nd=str(newday2)
else:
nd='0'+str(newday2)
newmonth2=month+1
if len(str(newmonth2))>1:
nm=str(newmonth2)
else:
nm='0'+str(newmonth2)
newdate=(str(year)+'-'+nm+'-'+nd)
else:
pass
elif monthdays==30:
if day<10:
newday4=day+21
if len(str(month))>1:
m1=str(month)
else:
m1='0'+str(month)
newdate=(str(year)+'-'+m1+'-'+str(newday4))
elif day>=10:
newday5=day-9
if len(str(newday5))>1:
nd1=str(newday5)
else:
nd1='0'+str(newday5)
newmonth3=month+1
if len(str(newmonth3))>1:
nm1=str(newmonth3)
else:
nm1='0'+str(newmonth3)
newdate=(str(year)+'-'+nm1+'-'+nd1)
else:
pass
elif monthdays==29:
if day<9:
newday6=day+21
newdate=(str(year)+'-'+'02'+'-'+str(newday6))
elif day>=9:
newday7=day-8
if len(str(newday7))>1:
nd2=str(newday7)
else:
nd2='0'+str(newday7)
newmonth4=month+1
if len(str(newmonth4))>1:
nm2=str(newmonth4)
else:
nm2='0'+str(newmonth4)
newdate=(str(year)+'-'+nm2+'-'+nd2)
else:
pass
elif monthdays==28:
if day<8:
newday8=day+21
newdate=(str(year)+'-'+'02'+'-'+str(newday8))
elif day>=8:
newday9=day-7
if len(str(newday9))>1:
nd3=str(newday9)
else:
nd3='0'+str(newday9)
newmonth5=month+1
if len(str(newmonth5))>1:
nm3=str(newmonth5)
else:
nm3='0'+str(newmonth5)
newdate=(str(year)+'-'+nm3+'-'+nd3)
else:
pass
else:
pass
delay=0
run='select(date(now()))'
cur.execute(run)
r=cur.fetchall()
today=str(r[0][0])
print()
delay=0
else:
delay=1
cur.execute(st)
cat=cur.fetchone()
if delay==0:
print('No delay')
else:
pass
except:
print()
#returned books
def bookreturn():
try:
cur.execute(run)
db.commit()
cur.execute(inc)
db.commit()
print()
except:
print('An unexpected error occurred, please try again')
db.rollback()
print()
#Main Code
try:
except:
print()
1. The records for all books in the library are saved in the "Books" table
2. The records for people issuing the books are saved in the "issue_records"
table
4. A book must be returned within 3 weeks of issuing else the issuer (if a
student) will have to pay a return delay fine of Rs 400.''')
dbcheck=input("Do you already have the database 'library' created? y/n: ")
try:
if dbcheck.upper()=='Y':
cur.execute("USE library;")
print("'library' database in use.")
elif dbcheck.upper()=='N':
cur.execute('USE library')
db.commit()
category char(1),
cur.execute(itable)
db.commit()
B_author varchar(40),
B_genre varchar(40),
cur.execute(btable)
db.commit()
else:
except:
if dbcheck.upper()=='Y':
print("database 'library' does not exist.")
else:
#MAIN MENU
while True:
print()
6. Exit''')
print()
print()
if ch==1:
while True:
1. Add books
print()
addbk()
print()
elif nch==2:
change()
print()
elif nch==3:
delbook()
print()
elif nch==4:
print()
break
print()
while True:
print()
if nch==1:
viewallbooks()
print()
elif nch==2:
search()
print()
elif nch==3:
author()
print()
elif nch==4:
genre()
print()
elif nch==5:
print()
break
else:
print('Invalid input')
elif ch==3:
while True:
print()
if nch==1:
addrec()
print()
elif nch==2:
delissue()
print()
elif nch==3:
bookreturn()
print()
elif nch==4:
print()
break
else:
print('Invalid input')
print()
elif ch==4:
while True:
print()
viewallissues()
print()
elif nch==2:
checkrecords()
print()
elif nch==3:
findwho()
print()
elif nch==4:
showselrec()
print()
elif nch==5:
dateissue()
print()
elif nch==6:
print()
break
else:
print('Invalid input')
print()
elif ch==5:
checkdelay()
print()
elif ch==6:
print('Program terminated.')
break
else:
print()
OUTPUT