Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5516cfa

Browse files
authored
Update School Library Management.py
Added Comments and improved 1 line of code
1 parent 21d91bd commit 5516cfa

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

School Library Management/School Library Management.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#===================================||
2-
# School Library Management Project ||
3-
#===================================||
1+
#============================================================================================================================================================||
2+
# School Library Management Project ||
3+
#============================================================================================================================================================||
4+
5+
#Importing Modules
46

57
import time
68
import os
@@ -10,28 +12,28 @@ def database():
1012
print("============================================================================")
1113
print("\t\t\t Database Menu")
1214
print("============================================================================")
13-
print("Welcome User. Choose the Desired Action.")
14-
cursor.execute("show databases;")
15-
databases = cursor.fetchall()
16-
dbs = {}
15+
print("Welcome User. Choose the Desired Action.") #Menu
16+
cursor.execute("show databases;") #Executed SQL Query to get DataBases Name
17+
databases = cursor.fetchall() #Stored all the DataBases Names in variable
18+
dbs = {} #Created dictionary
1719
n = 1
18-
for i in databases :
20+
for i in databases : #Looping to print the databases names
1921
dbs[n] = i[0]
2022
print(str(n) + ". " + i[0])
2123
n += 1
22-
dbs[n] = "Create A New DataBase"
23-
dbs[n + 1] = "Exit From The Connection"
24+
dbs[n] = "Create A New DataBase" #Creating 2 more options and
25+
dbs[n + 1] = "Exit From The Connection" #assigning these in dictionary dbs
2426
print("****************************************************************************")
2527
print(str(n) + ". " + "Create A New DataBase")
2628
print(str(n + 1) + ". " + "Exit From The Connection")
2729
decision = 0
2830
while decision not in range(1, n + 1) :
29-
decision = int(input("Enter your choice => "))
30-
if decision in range(1, n) :
31+
decision = int(input("Enter your choice => ")) #If decision is not any of the specified options, input again
32+
if decision in range(1, n) :
3133
query = "USE " + dbs[decision]
32-
cursor.execute(query)
34+
cursor.execute(query) #Selected DB will be used
3335
os.system("cls")
34-
tables(dbs[decision])
36+
tables(dbs[decision]) #Selected DB's Tables Menu will be opened
3537
elif decision == n :
3638
new_db_name = input("Enter The New DataBase Name(A-Z,a-z,0-9,_) => ")
3739
if new_db_name == "" :
@@ -40,7 +42,7 @@ def database():
4042
dec = input("Are you sure you want to Create This DataBase?(y/n) => ")
4143
if dec == "y":
4244
query = "create database " + new_db_name
43-
cursor.execute(query)
45+
cursor.execute(query) #Creating new DB
4446
cnx.commit()
4547
print("Successfully created '" + new_db_name + "' DataBase...")
4648
input("Press Enter to go to DataBase Menu...")
@@ -51,7 +53,7 @@ def database():
5153
exit_decision = input("Are you sure you want to Exit?(y/n) => ")
5254
if exit_decision == "y" :
5355
cnx.close()
54-
print("See ya later.")
56+
print("See ya later.") #Exit
5557
time.sleep(3)
5658
quit()
5759
else :
@@ -68,17 +70,17 @@ def tables(db) :
6870
print("============================================================================")
6971
cursor.execute("show tables")
7072
tables = cursor.fetchall()
71-
n = 1
73+
n = 1 #Printing all Tables Names
7274
for i in tables :
7375
print(str(n) + ". " + i[0])
7476
n += 1
7577
print("****************************************************************************")
7678
print("This Python Program Will Now Use These 3 Tables in this DataBase:")
7779
print("$. School_Students $. Library_Books $. Issued_Books")
78-
print("If these tables exist already with the Specified Columns in ReadMe, use")
79-
print("them directly. Otherwise Create these Tables.")
80+
print("If these tables exist already with the Specified Columns in ReadMe, use") #Before creating these 3 tables, check the Table Structure
81+
print("them directly. Otherwise Create these Tables.") #from the ReadMe
8082
decision = input("Do you want to Create these Tables here?(y/n) => ")
81-
if decision == "y":
83+
if decision == "y": #Creating Tables via SQL Query
8284
cursor.execute("""CREATE TABLE IF NOT EXISTS issued_books (
8385
Accession_No int NOT NULL PRIMARY KEY CHECK (Accession_No > 0),
8486
Book_Name varchar(200) NOT NULL,
@@ -100,7 +102,7 @@ def tables(db) :
100102
)""")
101103
cnx.commit()
102104
print("Tables Created Successfully...")
103-
decision = input("Do you want to Use these Tables here?(y/n) => ")
105+
decision = input("Do you want to Use these Tables here?(y/n) => ") #Use the Tables in Menu
104106
if decision == "y" :
105107
os.system("cls")
106108
menu()
@@ -127,7 +129,7 @@ def menu() :
127129
if decision == "1" :
128130
os.system("cls")
129131
library()
130-
elif decision == "2" :
132+
elif decision == "2" : #Library and School Menus can be accessed
131133
os.system("cls")
132134
school()
133135
elif decision == "3" :
@@ -174,7 +176,7 @@ def library() :
174176
os.system("cls")
175177
modify_book()
176178
elif decision == "5" :
177-
os.system("cls")
179+
os.system("cls") #Library Menu and its Functions
178180
issue_book()
179181
elif decision == "6" :
180182
os.system("cls")
@@ -209,7 +211,7 @@ def school() :
209211
os.system("cls")
210212
show_students()
211213
elif decision == "2" :
212-
os.system("cls")
214+
os.system("cls") #School Menu and its Functions
213215
add_student()
214216
elif decision == "3" :
215217
os.system("cls")
@@ -230,10 +232,10 @@ def show_books() :
230232
data = cursor.fetchall()
231233
n = 1
232234
for row in data :
233-
print(str(n) + ". Accesion No. : {0}\n Book Name : {1}\n Author : {2}\n Price : {3}".format(row[0], row[1], row[2], row[3]))
234-
n += 1
235-
print("****************************************************************************")
236-
input("Press Enter to go back to the Library...")
235+
print(str(n) + ". Accesion No. : {0}\n Book Name : {1}\n Author : {2}\n Price : {3}".format(row[0], row[1], row[2], row[3])) #Printing, Accession
236+
n += 1 #No., Book Name,
237+
print("****************************************************************************") #Author, Price of
238+
input("Press Enter to go back to the Library...") #each book
237239
os.system("cls")
238240
library()
239241

@@ -245,8 +247,8 @@ def add_book() :
245247
print("============================================================================")
246248
cursor.execute("select accession_no, book_name from library_books;")
247249
data = cursor.fetchall()
248-
a = [ ]
249-
b = [ ]
250+
a = [ ] #List to store all accession no
251+
b = [ ] #List to store all book name
250252
for i in data :
251253
a.append(i[0])
252254
b.append(i[1])
@@ -303,7 +305,7 @@ def delete_book() :
303305
print("============================================================================")
304306
cursor.execute("select accession_no from library_books;")
305307
data = cursor.fetchall()
306-
a = [ ]
308+
a = [ ] #List to store all accession no. from library_books
307309
for i in data :
308310
a.append(i[0])
309311
acc_no = int(input("Enter the Accession No. => "))
@@ -316,7 +318,7 @@ def delete_book() :
316318
query = "select book_name, author, price_in_rs from library_books where accession_no = (%s)"
317319
cursor.execute(query, data)
318320
data = cursor.fetchall()
319-
b = [ ]
321+
b = [ ] #List to store all other info of book to be deleted
320322
for i in data :
321323
b.append(i[0])
322324
b.append(i[1])
@@ -328,7 +330,7 @@ def delete_book() :
328330
if decision == "y" :
329331
cursor.execute("select accession_no from issued_books;")
330332
data = cursor.fetchall()
331-
c = [ ]
333+
c = [ ] #List to store all accession no from issued_books
332334
for i in data :
333335
c.append(i[0])
334336
if acc_no in c :
@@ -761,20 +763,19 @@ def modify_student() :
761763
school()
762764

763765

764-
username = input("Enter the User Name for MySQL Conenction => ")
766+
username = input("Enter the User Name for MySQL Conenction => ") #Authorization to connect to the MySQL in local machine
765767
password = input("Enter the Password to access the MySQL Connection => ")
766768
os.system("cls")
767769
try :
768-
cnx = mysql.connector.connect(host = "127.0.0.1", user = username, passwd = password)
770+
cnx = mysql.connector.connect(host = "127.0.0.1", user = username, passwd = password) #Trying to connect to MySQL and creating Connection Object
769771
except :
770-
print("Incorrect Password.")
772+
print("Incorrect Login Details.") #When fails authorization
771773
input("Press Enter to Close the Program...")
772774
exit
773775
if cnx :
774776
print(cnx)
775777
print("Successfully Connected...")
776-
cursor = cnx.cursor()
778+
cursor = cnx.cursor() #Created Cursor for Connection Object
777779
input("Press Enter to Access the Database Menu...")
778780
os.system("cls")
779781
database()
780-

0 commit comments

Comments
 (0)