-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
315 lines (291 loc) · 9.57 KB
/
main.py
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
#Modules:-
import os
import sys
#MYSQL Connection:-
try:
import mysql.connector as sql
except:
print("Errors:\n1) MySql is not Installed on System\n2) pip is not Installed\n3) MySQL is not connected with Python\n")
print("Please Refer to the Documentation for Fix this")
#Database Creation/Connection:-
pass_wd=str(input("Enter MySQL Password: "))
try:
db=sql.connect(host="localhost",user="root",passwd=pass_wd)
cursor=db.cursor()
if db.is_connected():
try:
cursor.execute("USE pyplm")
db.commit()
print("Database connected")
except:
cursor.execute("create database pyplm")
cursor.execute("USE pyplm")
print("New Database Created")
db.commit()
print("Database connected")
except:
print("Password is Wrong, Try Again")
pass_wd2=str(input("Re-Enter MySQL Password: "))
try:
db=sql.connect(host="localhost",user="root",passwd=pass_wd2)
cursor=db.cursor()
if db.is_connected():
try:
cursor.execute("USE pyplm")
db.commit()
print("Database connected")
except:
cursor.execute("create database pyplm")
cursor.execute("USE pyplm")
print("New Database Created")
db.commit()
print("Database connected")
except:
print("Password is Wrong, Try Again [Last Chance]")
pass_wd3=str(input("Re-Enter MySQL Password: "))
try:
db=sql.connect(host="localhost",user="root",passwd=pass_wd3)
cursor=db.cursor()
if db.is_connected():
try:
cursor.execute("USE pyplm")
db.commit()
print("Database connected")
except:
cursor.execute("create database pyplm")
cursor.execute("USE pyplm")
print("New Database Created")
db.commit()
print("Database connected")
except:
print(" ")
print("We are Unable to authorize You")
print(" ")
print("Press '1' to fix Errors")
fix_db=int(input("Enter: "))
print("")
if fix_db==1:
x=input("Enter Name of host (Default: localhost): ")
y=input("Enter Name of user (Default: root): ")
z=input("Enter Password: ")
try:
db=sql.connect(host=x,user=y,passwd=z)
cursor=db.cursor()
if db.is_connected():
try:
cursor.execute("USE pyplm")
db.commit()
print("Database connected")
except:
cursor.execute("create database pyplm")
cursor.execute("USE pyplm")
print("New Database Created")
db.commit()
print("Database connected")
except:
print("\n")
print("Unable to authorize")
print("Please Refer to the Documentation for Fix this")
print("Errors May Occurs:\n1) MySql is not Installed on System\n2) pip is not Installed\n3) MySQL is not connected with Python\n")
else:
print("Thank You for Use Our Service\nPlease give us your feedback to improve our service")
exit
#Password Manager:-
Admin_Passwd = "admin"
#First Time Run Setup:-
def Create_Table():
try:
Table="CREATE TABLE DATA (Student_ID INT(3), Book_ID INT(5))"
cursor.execute(Table)
db.commit()
except:
print("TABLE ALREADY EXIST")
os.system('cls' if os.name == 'nt' else "printf '\033c'")
#Credit:-
def credit():
os.system('cls' if os.name == 'nt' else "printf '\033c'")
print("Credit:-")
print("Python Librery Project")
print("Version: 1.0 BETA")
print("\nCode By: Yash Garg\nTeam Members: Dhruv Gupta, Apurva Bansal, Ritesh Kumar Sinha, Anshul Sirohi, Saurav Agrawal\n")
print("An Opne Source Project - Build for College Project 3rd SEM (SRM University Delhi-NCR)")
print("Contact US via E-Mail: [email protected]\n")
if (continue_Query()==1):
start_menu()
#Menu:-
def start_menu():
os.system('cls' if os.name == 'nt' else "printf '\033c'")
print("WELCOME\n")
print("1) Admin Login\n2) Student Login\nPress '-1' for Exit\n")
temp = int(input("Choose: "))
if temp==1:
pass_admin = str(input("Enter Admin Password: "))
if (pass_admin==Admin_Passwd):
Admin_Panel()
else:
print("Wrong Password!")
elif temp==2:
Student_Panel()
elif temp==0:
credit()
elif temp==-1:
exit_prog()
else:
print("Wrong inpur")
start_menu()
def Admin_Panel():
os.system('cls' if os.name == 'nt' else "printf '\033c'")
print("Admin Portal\n")
print("1) View All Books Issued\n2) Edit Issued Books Data\n3) Delete Record\n4) Student Portal")
temp = int(input("Choose: "))
if temp==1:
View_Table_Data()
elif temp==2:
Edit_Table_Data()
elif temp==3:
Delete_Row()
elif temp==4:
Student_Panel()
elif temp==0:
start_menu()
else:
print("Wrong Input")
if continue_Query()==1:
Admin_Panel()
else:
start_menu()
if (continue_Query()==1):
Admin_Panel()
else:
exit_prog()
def Student_Panel():
os.system('cls' if os.name == 'nt' else "printf '\033c'")
print("WELCOME TO LIBRERY\n")
print("1) Issue a New Book\n2) Return a Book\n3) View all Issued Book")
temp = int(input("Choose: "))
if temp==1:
Issue_Book()
elif temp==2:
Return_Book()
elif temp==3:
View_Student_Issue_Book()
elif temp==0:
start_menu()
else:
Student_Panel()
if (continue_Query()==1):
Student_Panel()
else:
exit_prog()
#Commands:-
def continue_Query():
print("Press '1' To Return")
try:
cont_que = int(input("Enter: "))
if cont_que==1:
return 1
else:
return 0
except:
start_menu()
def exit_prog():
cursor.close()
db.close()
sys.exit()
#Admin Panel Commands:-
def View_Table_Data():
view = "SELECT * FROM DATA"
cursor.execute(view)
results = cursor.fetchall()
print("+------------+---------------+\n| Student ID | Book ID |\n+------------+---------------+")
for i in results:
j=str(i).split()
for k in j:
print(k,end=" | ")
print()
print("+------------+---------------+")
def Edit_Table_Data():
print("\n1) Edit Book ID\n2) Return Back\n")
temp = int(input("Choose: "))
if temp==1:
StudentID = int(input("Enter Student ID: "))
BookID = int(input("Enter Correct Book ID: "))
query = "UPDATE DATA SET Book_ID = '%d' WHERE Student_ID = '%d'" % (BookID, StudentID)
cursor.execute(query)
db.commit()
elif temp==2:
Admin_Panel()
else:
print("Wrong Input!")
if continue_Query()==1:
Admin_Panel()
else:
start_menu()
def Delete_Row():
StudentID = int(input("Enter Student ID: "))
query = "DELETE FROM DATA WHERE Student_ID = '%d'" % (StudentID)
cursor.execute(query)
db.commit()
if continue_Query()==1:
Admin_Panel()
else:
start_menu
#Student Panel Commands:-
def Issue_Book():
try:
x = int(input("Enter Student ID: "))
y = int(input("Enter Book UPC Code: "))
query = "INSERT INTO DATA (Student_ID, Book_ID) VALUES ('%d','%d')" % (x,y)
cursor.execute(query)
db.commit()
print("Student ID: '%d'\nBook ID: '%d' \nhas been Issue Successfully\n" % (x,y))
if continue_Query()==1:
Student_Panel()
else:
Issue_Book()
except:
print("WRONG INPUT! ENTER Numbers Only\n")
if continue_Query == 1:
Issue_Book()
else:
Student_Panel()
def Return_Book():
try:
x = int(input("Enter Student ID: "))
y = int(input("Enter Book ID which has to be Return: "))
query = "DELETE FROM DATA WHERE Student_ID = '%d' AND Book_ID = '%d'" % (x,y)
cursor.execute(query)
db.commit()
print("\n")
if continue_Query() == 1:
Student_Panel()
else:
Return_Book()
except:
print("WRONG INPUT!\n")
if continue_Query() == 1:
Student_Panel()
else:
Return_Book()
def View_Student_Issue_Book():
try:
x = int(input("Enter Student ID: "))
query = "SELECT * FROM DATA WHERE Student_ID = '%d'" % (x)
cursor.execute(query)
results = cursor.fetchall()
print("+------------+---------------+\n| Student ID | Book ID |\n+------------+---------------+")
for i in results:
j=str(i).split()
for k in j:
print(k,end=" | ")
print()
print("+------------+---------------+")
except:
print("Wrong Student ID!\n")
if continue_Query() == 1:
Student_Panel()
else:
View_Student_Issue_Book()
#Main Run:-
Create_Table()
start_menu()