-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
212 lines (184 loc) · 7.8 KB
/
Copy pathproject.py
File metadata and controls
212 lines (184 loc) · 7.8 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
# This project is created by Aswanth R
# For any reference or corrections please post and comment of out respo
# Here is link https://github.com/NodeX-AR/CS-Project/
# For better function of script please close with the code of project rather than manual closing !
import mysql.connector as sqlcon
#connection
db=sqlcon.connect(
host='localhost',
user='root',
password='password', # Change this
)
#checking connection
if db.is_connected() == False:
print("================== E R R O R =====================")
print("Please check your connection of MySQL with Python!")
print("To check your connection please run the connection")
print("test script given in the same respo and crosscheck")
print("error code and fix in our respo or google.")
print("GitHub Respo Link =>")
print("https://github.com/Nodex-AR/CS-Project")
#checking configs for project
cursor=db.cursor()
cursor.execute("show databases")
t_db=cursor.fetchall()
configdb = ('library',)
if configdb in t_db:
print("All Configs already set !")
cursor.execute("USE library")
#doing configs for project
else:
print("Please wait till we make changes !")
cursor.execute("CREATE DATABASE library")
print("+---------------------------------------------------------+")
print("| SETIING CONFIGS.......... |")
print("+---------------------------------------------------------+")
print("| [0000000 ][ 20 % ] |")
cursor.execute("USE library")
print("| [00000000000000000 ][ 40 % ] |")
db.commit()
print("| [0000000000000000000000000 ][ 60 % ] |")
cursor.execute("CREATE table list ( student_id INT AUTO_INCREMENT PRIMARY KEY, student_name Varchar(50) , book Varchar(100) , status Varchar(30)) ")
print("| [0000000000000000000000000000000 ][ 80 % ] |")
db.commit()
print("| [000000000000000000000000000000000000000000000][ 100% ] |")
#registry section
def registry():
cursor.execute("SELECT * FROM `list`")
total_students = cursor.fetchall()
cursor.execute("SELECT * FROM `list` WHERE status = 'Book Issued'")
books_out = cursor.fetchall()
print("| Library Statistics |")
print("+---------------------------------------------------------+")
print("| Total Registered Students: ",len(total_students))
print("| Books Currently Issued: ",len(books_out))
#section to add a new student
def new():
n=input("Enter the name of new student:")
query = "INSERT INTO `list` (student_name) VALUES (%s)"
cursor.execute(query,(n,))
db.commit()
print("Student",n,"is sucessfully added.")
#section to return book
def return_book():
n = input("Enter the name of student:")
cursor.execute("SELECT status FROM `list` WHERE student_name = %s", (n,))
result = cursor.fetchone()
if result is None:
print("Student not found.")
return
if result[0] != "Book Issued":
print(f"{n} has no book to return.")
return
query = "UPDATE `list` SET status = 'Book Returned', book = NULL WHERE student_name = %s"
cursor.execute(query, (n,))
db.commit()
print("Student", n, "has returned the book. Status Updated!")
#section to issue a book
def issue():
n=input("Enter the name of new student:")
cursor.execute("SELECT status FROM `list` WHERE student_name = %s", (n,))
result = cursor.fetchone()
if result is None:
print("Student not found.")
return
elif result[0] == "Book Issued":
print("The student",n,"has already been issued a book.")
return
b=input("Enter the book name:")
query = "UPDATE `list` SET status = 'Book Issued', book = %s WHERE student_name = %s"
cursor.execute(query,(b,n))
db.commit()
print("Book",b,"issued to",n,"successfully!")
#section to check the status of a student
def status():
n=input("Enter the name of new student:")
query = "SELECT status,book FROM `list` WHERE student_name = %s"
cursor.execute(query,(n,))
result = cursor.fetchone()
if result is None:
print("Student not found.")
elif result[0] == "Book Issued":
print("The student",n,"has been issued a book ")
print("The book issued is ",result[1])
return
elif result[0] == "Book Returned":
print("The student",n,"has already returned the book.")
else:
print("The student",n,"has never taken a book.")
#search
def search_student():
name = input("Enter student name to search:")
query = "SELECT student_name, book, status FROM `list` WHERE student_name LIKE %s"
cursor.execute(query, (f"%{name}%",))
results = cursor.fetchall()
if results:
print(" Search Results ")
print("+---------------------------------------------------------+")
for row in results:
if row[1] is None:
book_display = "None"
else:
book_display = row[1]
print("Student:", row[0], ", Book:", book_display, ", Status:", row[2])
else:
print("No student found.")
#master reset section !
def reset():
ans = input("|Are You Sure About reset [Y/N] :").lower()
print("+---------------------------------------------------------+")
if ans == 'y':
cursor.execute("Drop database library")
print("|Reset Protocol Executed Successfully. Now Please restart!|")
db.commit()
print("+---------------------------------------------------------+")
else:
print("|Exiting Reset Protocol. Please restart this program ! |")
print("+---------------------------------------------------------+")
#main workflow
def main():
print("+---------------------------------------------------------+")
print("| LIBRARY MANAGEMENT SYSTEM |")
while True:
print("+---------------------------------------------------------+")
print("|Options : |")
print("+---------------------------------------------------------+")
print("|1.Issue of book. |")
print("|2.Show the registry. |")
print("|3.Add new student. |")
print("|4.Return of book. |")
print("|5.Check the status of a student. |")
print("|6.Search for a student. |")
print("|7.Close the system. |")
print("|8.Master Reset. |")
print("+---------------------------------------------------------+")
opt=input("|Enter the option index number :")
print("+---------------------------------------------------------+")
if opt == "1" :
issue()
elif opt == "2":
registry()
elif opt == "3":
new()
elif opt == "4":
return_book()
elif opt == "5":
status()
elif opt == "6":
search_student()
elif opt == "7":
break
elif opt == "8":
reset()
break
else:
print("Please enter a valid option")
continue
print("| Thank You using our program :) |")
print("+---------------------------------------------------------+")
cursor.close()
db.close()
#start section
if __name__ == "__main__":
main()
#Please don't do any change without knowing what your are doing !