-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_management.py
More file actions
600 lines (526 loc) · 24.3 KB
/
Copy pathlibrary_management.py
File metadata and controls
600 lines (526 loc) · 24.3 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import os
import mysql.connector as sc
import datetime as dt
# Function to Delete/truncate data from Library
def truncate_data():
os.system('cls')
print('************************************************************')
print('* Truncate Library Data *')
print('************************************************************')
print('* 1. Delete all Books *')
print('* 2. Delete all Members *')
print('* 3. Delete all Registers (Issued Books) *')
print('* 4. Delete ALL (Books, Members, Registers) *')
print('* 0. Cancel *')
print('************************************************************')
try:
choice = int(input(' Enter Option :- '))
if choice == 0:
print("Cancelled.")
return
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
if choice == 1:
cur.execute("TRUNCATE TABLE book")
con.commit()
print("All books deleted.")
elif choice == 2:
cur.execute("TRUNCATE TABLE member")
con.commit()
print("All members deleted.")
elif choice == 3:
cur.execute("TRUNCATE TABLE register")
con.commit()
print("All register records deleted.")
elif choice == 4:
cur.execute("TRUNCATE TABLE register")
cur.execute("TRUNCATE TABLE book")
cur.execute("TRUNCATE TABLE member")
con.commit()
print("All data deleted from library.")
else:
print("Invalid option.")
con.close()
except Exception as e:
print("Error during truncation:", e)
input("Press any key to continue...")
#Function to check availibity of a Book no.
def checkbook(bno):
try:
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
cur.execute("Select count(*) from book where bno=%s", (bno,))
data = cur.fetchall()
d = data[0][0]
con.close()
return d
except Exception:
return 0
#Function to check availibity of a Member no..
def checkmember(idd):
try:
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
cur.execute("Select count(*) from member where id=%s", (idd,))
data = cur.fetchall()
d = data[0][0]
con.close()
return d
except Exception:
return 0
#Function to check availibity of a issue no.
def checkissue(iss):
try:
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
cur.execute("Select count(*) from register where is_no=%s", (iss,))
data = cur.fetchall()
d = data[0][0]
con.close()
return d
except Exception:
return 0
#Function to check book with member.
def checki_book_member(iss):
try:
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
cur.execute("Select count(*) from register where id=%s", (iss,))
data = cur.fetchall()
d = data[0][0]
con.close()
return d
except Exception:
return 0
#Function to check availibity of a issue no.
def checkqty(bno):
try:
con = sc.connect(host='localhost', user='root', password='12345678', database='library')
cur = con.cursor()
cur.execute("Select copy from book where bno=%s", (bno,))
data = cur.fetchone()
d = data[0] if data else 0
con.close()
return d
except Exception:
return 0
# Function to Register a Book
def book():
os.system('cls')
ch = 'y'
while ch.lower() == 'y':
con = sc.connect(host='localhost', database='library', user='root', password='12345678')
cur = con.cursor()
try:
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
bno = int(input(" Enter Book No. \t\t\t: "))
bno = int("102" + str(bno))
while checkbook(bno):
print(" Entered Book No. already Exists ")
bno = int(input(" Enter Book No. \t\t\t: "))
bno = int("102" + str(bno))
bname = input(" Enter Book Name \t\t\t: ")
author = input(" Enter Author Name \t\t\t: ")
price = float(input(" Enter Book Price \t\t\t: "))
qty = int(input(" Enter Quantity \t\t\t: "))
cur.execute("""
INSERT INTO book (bno, bname, author, price, copy)
VALUES (%s, %s, %s, %s, %s)
""", (bno, bname, author, price, qty))
con.commit()
print('************************************************************')
print("\t\tBook Successfully Registered")
print('************************************************************')
ch = input("Do you want to register another book? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during registering book.")
print('************************************************************')
ch = input("Do you want to try with another data? (y/n): ")
# Function to Register a Member
def member():
os.system('cls')
ch = 'y'
while ch.lower() == 'y':
con = sc.connect(host='localhost', database='library', user='root', password='12345678')
cur = con.cursor()
try:
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
while checkmember(mno):
print(" Entered Member No. already Exists ")
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
name = input(" Enter Member Name \t\t\t: ")
mob = input(" Enter Mobile No. \t\t\t: ")
df = dt.date.today()
jdate = df.strftime("%Y-%m-%d")
cur.execute("""
INSERT INTO member (id, name, join_date, mob)
VALUES (%s, %s, %s, %s)
""", (mno, name, jdate, mob))
con.commit()
print('************************************************************')
print("\t\tMember Registered Successfully")
print('************************************************************')
ch = input("Do you want to register another member? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during Member Registration.")
print('************************************************************')
ch = input("Do you want to try with another data? (y/n): ")
# Function to Issue a Book
def register():
os.system('cls')
ch = 'y'
while ch.lower() == 'y':
con = sc.connect(host='localhost', database='library', user='root', password='12345678')
cur = con.cursor()
try:
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
while checkissue(ino):
print(" Entered Issue No. already Exists ")
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
bno = int(input(" Enter Book no. \t\t\t: "))
bno = int("102" + str(bno))
while checkbook(bno) == 0 or checkqty(bno) == 0:
print(" Entered Book No. not available or out of stock ")
bno = int(input(" Enter Book no. \t\t\t: "))
bno = int("102" + str(bno))
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
while checkmember(mno) == 0:
print(" Entered Member does not Exist ")
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
df = dt.date.today()
idate = df.strftime("%Y-%m-%d")
dew = input(" Enter Due date (yyyy-mm-dd) \t: ")
cur.execute("""
INSERT INTO register (is_no, bno, id, issue_date, dew_date)
VALUES (%s, %s, %s, %s, %s)
""", (ino, bno, mno, idate, dew))
con.commit()
cur.execute("UPDATE book SET copy = copy - 1 WHERE bno = %s", (bno,))
con.commit()
print('************************************************************')
print("\t\tBook Issued Successfully")
print('************************************************************')
ch = input("Do you want to issue more books? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during Issuing Book.")
print('************************************************************')
ch = input("Do you want to try with another data? (y/n): ")
# Function to View Member Details
def view():
ch = 'y'
while ch.lower() == 'y':
try:
con = sc.connect(
host='localhost',
database='library',
user='root',
password='12345678'
)
cur = con.cursor()
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
while checkmember(mno) == 0:
print(" Entered Member Not Exists ")
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
cur.execute("""
SELECT name, bname, issue_date, dew_date, return_date, mob
FROM book b, register r, member m
WHERE b.bno = r.bno AND m.id = r.id AND m.id = %s
""", (mno,))
data = cur.fetchone()
os.system('cls')
print('************************************************************')
print('* Member Details *')
print('************************************************************')
print(' Member No. :', mno)
if data:
print(' Member Name :', data[0])
print(' Book Name :', data[1])
print(' Issue Date :', data[2])
print(' Due Date :', data[3])
print(' Return Date :', data[4])
print(' Mobile :', data[5])
else:
print(' No records found for this member.')
print('************************************************************')
ch = input("Do you want to view another record? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during viewing.")
print('************************************************************')
ch = input("Do you want to try with another data? (y/n): ")
# Function to view list of Books present in library
def list_all_book():
ch = 'y'
while ch == 'y' or ch == 'Y':
try:
con = sc.connect(host='localhost', database='library', user='root', password='12345678')
cur = con.cursor()
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
cur.execute("SELECT bno, bname, copy FROM book")
data = cur.fetchall()
os.system('cls')
print('************************************************************')
print('* List of Books *')
print('************************************************************')
print("{:<10} {:<40} {:<5}".format("Book No.", "Book Name", "Qty"))
print('-' * 60)
for row in data:
print("{:<10} {:<40} {:<5}".format(row[0], row[1], row[2]))
print('************************************************************')
ch = input("Press any key to Continue...")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during Viewing.")
print('************************************************************')
ch = input("\tDo you want to try with another data?(y/n)")
def list_all_members():
try:
con = sc.connect(
host='localhost',
database='library',
user='root',
password='12345678'
)
cur = con.cursor()
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
cur.execute("SELECT id, name, mob FROM member")
data = cur.fetchall()
os.system('cls')
print('************************************************************')
print('* List of Members *')
print('************************************************************')
print(f"{'Member No.':<15}{'Member Name':<25}{'Mobile'}")
print('------------------------------------------------------------')
for row in data:
print(f"{row[0]:<15}{row[1]:<25}{row[2]}")
print('************************************************************')
input("Press any key to Continue...")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during Viewing.")
print('************************************************************')
# Function to view list of Books issued with member name
def list_all():
ch = 'y'
while ch.lower() == 'y':
try:
con = sc.connect(
host='localhost',
database='library',
user='root',
password='12345678'
)
cur = con.cursor()
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
cur.execute("""
SELECT is_no, name, bname, dew_date
FROM book b, register r, member m
WHERE b.bno = r.bno AND m.id = r.id
""")
data = cur.fetchall()
os.system('cls')
print('************************************************************')
print('* List of Books Issued *')
print('************************************************************')
print(f'{"Issue No.":<12}{"Member Name":<20}{"Book Name":<30}{"Due Date":<15}')
print('-' * 80)
for row in data:
print(f'{str(row[0]):<12}{str(row[1]):<20}{str(row[2]):<30}{str(row[3]):<15}')
print('************************************************************')
ch = input("Press any key to Continue...")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during Viewing.")
print('************************************************************')
ch = input("\tDo you want to try with another data? (y/n): ")
# Function to Extend due Date or Cancel Membership
def extend():
ch = 'y'
while ch.lower() == 'y':
try:
con = sc.connect(
host='localhost',
database='library',
user='root',
password='12345678'
)
cur = con.cursor()
os.system('cls')
print('************************************************************')
print('* Library Management System *')
print('************************************************************')
print('* *')
print('* 1. Extend Due Date *')
print('* 2. Cancel Membership *')
print('* *')
print('************************************************************')
opt = int(input(' Enter Option :- '))
print('************************************************************')
if opt == 1:
os.system('cls')
list_all()
print("\n")
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
while checkissue(ino) == 0:
print(" Entered Issue No. does not exist ")
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
print('************************************************************')
dew = input("\t\tEnter new Due Date (YYYY-MM-DD): ")
cur.execute("UPDATE register SET dew_date = %s WHERE is_no = %s", (dew, ino))
con.commit()
print('************************************************************')
print("\t\tDue Date Successfully Extended")
print('************************************************************')
elif opt == 2:
list_all_members()
print("\n")
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
while checkmember(mno) == 0:
print(" Entered Member does not exist ")
mno = int(input(" Enter Member No. \t\t\t: "))
mno = int("101" + str(mno))
while checki_book_member(mno):
print('************************************************************')
print("\tBooks pending = ", checki_book_member(mno))
print("\tPlease return all books first.")
print('************************************************************')
input("Press any key to return books...")
ret_book()
cur.execute("DELETE FROM member WHERE id = %s", (mno,))
con.commit()
print('************************************************************')
print("\t\tMembership Successfully Cancelled")
print('************************************************************')
ch = input("\tDo you want to update anything else? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during updating.")
print('************************************************************')
ch = input("\tDo you want to try with another data? (y/n): ")
# Function to Return a Book
def ret_book():
ch = 'y'
while ch.lower() == 'y':
try:
con = sc.connect(
host='localhost',
database='library',
user='root',
password='12345678'
)
cur = con.cursor()
os.system('cls')
list_all()
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
while checkissue(ino) == 0:
print(" Entered Issue No. does not exist ")
ino = int(input(" Enter Issue No. \t\t\t: "))
ino = int("103" + str(ino))
cur.execute(
"UPDATE book SET copy = copy + 1 WHERE bno IN (SELECT bno FROM register WHERE is_no = %s)",
(ino,)
)
con.commit()
cur.execute("DELETE FROM register WHERE is_no = %s", (ino,))
con.commit()
print('************************************************************')
print('* Book Returned Successfully *')
print('************************************************************')
ch = input(" Do you want to return another book? (y/n): ")
con.close()
except Exception:
print('************************************************************')
print("\t\tError!!!... during returning book.")
print('************************************************************')
ch = input(" Do you want to try with another data? (y/n): ")
#Main
if __name__ == "__main__":
ch = 1
while ch != 0:
os.system('cls')
print('*' * 66)
print('*{:^64}*'.format('Library Management System'))
print('*' * 66)
print('*{:64}*'.format(''))
print('*{:>20}. {:<42}*'.format('1', 'Book Entry'))
print('*{:>20}. {:<42}*'.format('2', 'Register Member'))
print('*{:>20}. {:<42}*'.format('3', 'Issue Book'))
print('*{:>20}. {:<42}*'.format('4', 'View Member issued Books'))
print('*{:>20}. {:<42}*'.format('5', 'View All Books'))
print('*{:>20}. {:<42}*'.format('6', 'View All Members'))
print('*{:>20}. {:<42}*'.format('7', 'View All issued Books'))
print('*{:>20}. {:<42}*'.format('8', 'Update details'))
print('*{:>20}. {:<42}*'.format('9', 'Return Book'))
print('*{:>20}. {:<42}*'.format('0', 'Exit'))
print('*{:64}*'.format(''))
print('*' * 66)
ch = int(input(' Enter Option :- '))
if ch == 1:
book()
elif ch == 2:
member()
elif ch == 3:
register()
elif ch == 4:
view()
elif ch == 5:
list_all_book()
elif ch == 6:
list_all_members()
elif ch == 7:
list_all()
elif ch == 8:
extend()
elif ch == 9:
ret_book()
elif ch == 0:
print("\n\nThanks For Visiting")
elif ch== 100:
truncate_data()