-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource_Code_Python_MySQL_CLASS-XII_Project.py
More file actions
565 lines (438 loc) · 22.6 KB
/
Source_Code_Python_MySQL_CLASS-XII_Project.py
File metadata and controls
565 lines (438 loc) · 22.6 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
import os
import mysql.connector
import datetime
print('''\n\t\t\t--- HEY USER, WELCOME TO i MUSIC INSTITUTE MANAGEMENT SYSTEM ---\n''')
user=input('Enter Your SQL User : ')
pw=input('Enter Your SQL Password : ')
#preparatory processes
def setup_database():
cmd='create database if not exists iMusic;'
cursor.execute(cmd)
print('Database Created...\n')
cmd='use imusic;'
cursor.execute(cmd)
print('Database Changed/Selected...\n')
cmd='create table if not exists Learners(sid varchar(4) primary key,sname char(30) not null,genre varchar(30),phone char(10),area varchar(30));'
cursor.execute(cmd)
print('Learners Table Created...\n')
cmd='create table if not exists Staff(staff_id varchar(4),staff_name varchar(30) not null,Designation varchar(30) not null,date_of_join date not null,salary int(5) not null);'
cursor.execute(cmd)
print('Staff Table Created...\n')
cmd='create table if not exists ResourcesIssued(sid varchar(4) not null,rname varchar(30) not null,issue_date date,rid varchar(4) not null);'
cursor.execute(cmd)
print('ResourcesIssued Table Created...\n')
cmd='create table if not exists ResourcesAvailable(rid varchar(4) primary key,r_name varchar(30) not null,available int(3) not null,issued int(3) not null);'
cursor.execute(cmd)
print('ResourceAvailable Table Created...\n')
def raw_insert():
#learners
a="insert into learners values('101','Neeraj','HipHop','7008674318','Palam');"
b="insert into learners values('102','Suraj','Jazz','7908674312','Manglapuri');"
c="insert into learners values('103','Dhiraj','Rock','8608431765','Dwarka');"
d="insert into learners values('104','Damini','Rock','9758423316','Shankar Vihar');"
e="insert into learners values('105','Kamlesh','Classical','9435104652','Dwarka');"
cursor.execute(a)
cursor.execute(b)
cursor.execute(c)
cursor.execute(d)
cursor.execute(e)
#staff
f="insert into staff values('101','Raju','Instructor','2020-02-02',25000);"
g="insert into staff values('102','Surya','Manager','2018-05-21',45000);"
h="insert into staff values('103','Dhirendar','Receptionist','2019-07-30',22500);"
i="insert into staff values('104','Dhamu','Peon','2021-01-31',12000);"
j="insert into staff values('105','Kaleen','Instructor','2020-06-22',26000);"
cursor.execute(f)
cursor.execute(g)
cursor.execute(h)
cursor.execute(i)
cursor.execute(j)
#resourcesissued
k="insert into ResourcesIssued values('101','Flute','2021-02-01','1');"
l="insert into ResourcesIssued values('104','Guitar','2021-01-27','2');"
m="insert into ResourcesIssued values('101','Flute Basics By Jonhard','2021-02-01','3');"
n="insert into ResourcesIssued values('104','Next Level : Guitarist','2021-01-27','4');"
o="insert into ResourcesIssued values('103','Mouth-Organ Basics','2020-10-21','5');"
cursor.execute(k)
cursor.execute(l)
cursor.execute(m)
cursor.execute(n)
cursor.execute(o)
#resourcesavailable
p="insert into ResourcesAvailable values ('1','Flute',26,12)"
q="insert into ResourcesAvailable values ('2','Guitar',24,2)"
r="insert into ResourcesAvailable values ('3','Flute Basics By Jonhard',21,31)"
s="insert into ResourcesAvailable values ('4','Next Level : Guitarist',26,12)"
t="insert into ResourcesAvailable values ('5','Mouth-Organ Basics',21,7)"
u="insert into ResourcesAvailable values ('6','Casio Small',21,0)"
v="insert into ResourcesAvailable values ('7','Veena',25,0)"
w="insert into ResourcesAvailable values ('8','Harmonium',10,0)"
cursor.execute(p)
cursor.execute(q)
cursor.execute(r)
cursor.execute(w)
cursor.execute(s)
cursor.execute(t)
cursor.execute(u)
cursor.execute(v)
temp_conn.commit()
#temporary connection to setup database
temp_conn=mysql.connector.connect(host='localhost',user=user,passwd=pw,charset='utf8')
cursor=temp_conn.cursor()
ask=input('Press 1 To Setup Database If Logining First Time Else Press Other Key : ')
if ask=='1':
setup_database()
print('Database Is Now Ready To Use')
raw_insert()
print('Data Inserted In Respective Tables')
temp_conn.close()
#main
mydb=mysql.connector.connect(host='localhost',user=user,passwd=pw,database='iMusic',charset='utf8')
cursor=mydb.cursor()
check_conn=mydb.is_connected()
try:
if check_conn==True:
print('\nConnection Established..')
except Exception as error:
print(error)
def show_tables():
cmd='show tables;'
cursor.execute(cmd)
rec=cursor.fetchall()
print('Table Inside iMusic Database')
print('-----------------------------')
for table in rec:
print(table[0])
def table_schema():
tablename=int(input('To Know The Structure(Schema), Press\n1. For Learners Table\n2. For ResourcesIssued Tables\n3. For ResourcesAvailable Table\n4. For Staff Tables\nEnter Your Choice : '))
if tablename==1:
cmd='desc learners;'
elif tablename==2:
cmd='desc resourcesissued;'
elif tablename==3:
cmd='desc resourcesavailable;'
elif tablename==4:
cmd='desc staff;'
else:
print('Invalid Input')
cursor.execute(cmd)
rec=cursor.fetchall()
l=['Field',' Type','Null','Key','Default','Extra']
for i in rec:
dic={l[0]:i[0],l[1]:i[1],l[1]:i[2],l[3]:i[3],l[4]:i[4],l[5]:i[5]}
print(dic)
def add_learner():
sid=input('Enter Learner ID : ')
sname=input('Enter Learner Name : ')
genre=input('Enter Genre Choosed For Learning : ')
phone=input('Enter Phone Number : ')
area=input('Enter Your Locality Name : ')
cmd='insert into learners values("{}","{}","{}","{}","{}")'.format(sid,sname,genre,phone,area)
cursor.execute(cmd)
mydb.commit()
print('Data Added..')
def add_staff():
sid=input('Enter Staff ID : ')
sname=input('Enter Staff Name : ')
des=input('Enter Designation : ')
doj=str(datetime.date.today())
sal=int(input('Enter Salary : '))
cmd='insert into staff values("{}","{}","{}","{}",{})'.format(sid,sname,des,doj,sal)
cursor.execute(cmd)
mydb.commit()
print('Data Added..')
def show_learners():
while True:
try:
print('1. Single Record Show By Sid\n2. List Learners Name Wise\n3. List Learners Genre Wise\n4. List Learners By Area\n5. Show All\n Another Numeric Key To Back Menu')
choose=int(input('Enter Your Choice : '))
if choose==1:
sid=input('Enter Learner ID To Show Record : ')
cmd='select * from learners where sid="{}"'.format(sid)
cursor.execute(cmd)
elif choose==2:
sname=input('Enter Name To Show Record(s) : ')
cmd='select * from learners where sname="{}"'.format(sname)
cursor.execute(cmd)
elif choose==3:
genre=input('Enter Genre To Show Record(s)')
cmd='select * from learners where genre="{}"'.format(genre)
cursor.execute(cmd)
elif choose==4:
area=input('Enter Area To Show Record(s)')
cmd='select * from learners where area="{}"'.format(area)
cursor.execute(cmd)
elif choose==5:
cmd='select * from learners;'
cursor.execute(cmd)
else:
break
except Exception as error:
print(error)
rec=cursor.fetchall()
if rec==[]:
print('No result Set')
else:
l=['sid','sname','genre','phone','area']
for i in rec:
dic={l[0]:i[0],l[1]:i[1],l[2]:i[2],l[3]:i[3],l[4]:i[4]}
print(dic)
def show_staff():
while True:
try:
print('1. Single Record Show By Sid\n2. List Staff Name Wise\n3. List Staff Designation Wise\n4. List Staff Salary Wise\n5. List Staff By Date Of Join\n6. Show All\n Another Numeric Key To Back Menu')
choose=int(input('Enter Your Choice : '))
if choose==1:
s_id=input('Enter Staff ID To Show Record : ')
cmd='select * from staff where staff_id="{}"'.format(s_id)
cursor.execute(cmd)
elif choose==2:
s_name=input('Enter Name To Show Record(s) : ')
cmd='select * from staff where staff_name="{}"'.format(s_name)
cursor.execute(cmd)
elif choose==3:
des=input('Enter Designation To Show Record(s)')
cmd='select * from staff where Designation="{}"'.format(des)
cursor.execute(cmd)
elif choose==4:
sal=int(input('Enter Salary To Show Record(s)'))
cmd='select * from staff where salary={}'.format(sal)
cursor.execute(cmd)
elif choose==5:
date=input('Enter Date In Format(YYYY-MM-DD) To Show Record(s)')
cmd='select * from staff where Date_of_join="{}"'.format(date)
cursor.execute(cmd)
elif choose==6:
cmd='select * from staff;'
cursor.execute(cmd)
else:
break
except Exception as error:
print(error)
rec=cursor.fetchall()
if rec==[]:
print('No result Set')
else:
l=['staff_id','staff_name','Designation','date_of_join','salary']
for i in rec:
dic={l[0]:i[0],l[1]:i[1],l[1]:i[2],l[3]:i[3],l[4]:i[4]}
print(dic)
def update_learner():
try:
sid=input('Enter Learner ID To Update : ')
sname=input('Enter Learner Name : ')
genre=input('Enter Genre Choosed For Learning : ')
phone=input('Enter Phone Number : ')
area=input('Enter Your Locality Name : ')
cmd='update learners set sname="{}",genre="{}",phone="{}",area="{}" where sid="{}"'.format(sname,genre,phone,area,sid)
cursor.execute(cmd)
mydb.commit()
print('Data Updated...')
except Exception as error:
print(error)
def update_staff():
try:
sid=input('Enter Staff ID To Update : ')
sname=input('Enter Staff Name : ')
des=input('Enter Designation : ')
sal=int(input('Enter Salary Number : '))
doj=input('Enter Date Of Join (YYYY-MM-DD) : ')
cmd='update staff set staff_name="{}",des="{}",sal={},doj="{}" where staff_id="{}"'.format(sname,des,sal,doj,sid)
cursor.execute(cmd)
mydb.commit()
print('Data Updated...')
except Exception as error:
print(error)
def delete_learner():
sid=input('Enter Learner ID To Delete : ')
cmd='delete from learners where sid="{}"'.format(sid)
cursor.execute(cmd)
mydb.commit()
print('Data Deleted...')
def delete_staff():
sid=input('Enter Staff ID To Delete : ')
cmd='delete from Staff where staff_id="{}"'.format(sid)
cursor.execute(cmd)
mydb.commit()
print('Data Deleted...')
def issue_new():
sid=input('Enter Learner ID : ')
rname=input('Enter Resource Name : ')
date=str(datetime.date.today())
rid=input('Enter Resource ID : ')
check_cmd='select available from resourcesavailable where rid="{}"'.format(rid)
cursor.execute(check_cmd)
rec=cursor.fetchall()
if rec==0:
print('All Resources Are Issued To Other Learners')
else:
cmd='insert into resourcesissued values("{}","{}","{}","{}")'.format(sid,rname,date,rid)
cursor.execute(cmd)
cmd='update resourcesavailable set available=available-1,issued=issued+1 where rid="{}"'.format(rid)
cursor.execute(cmd)
mydb.commit()
print('Resource Issued..')
def show_given_resources():
print('This Are The Detail Of Resources Issued & Issuer')
cmd='select * from resourcesissued;'
cursor.execute(cmd)
rec=cursor.fetchall()
l=['SID','Rname','Date','RID']
for i in rec:
print({l[0]:i[0],l[1]:i[1],l[2]:i[2],l[3]:i[3]})
def resources_return():
sid=input('Enter Learner ID To Collect : ')
rid=input('Enter RID(Of The Collecting Resource) : ')
check_cmd='select issued from resourcesavailable where rid="{}"'.format(rid)
cursor.execute(check_cmd)
rec=cursor.fetchall()
if rec==0:
print('You Can\'t Collect Resources That Are Not Yet Issued OR Not Belong To Institute')
else:
cmd='delete from resourcesissued where sid="{}" and rid="{}"'.format(sid,rid)
cursor.execute(cmd)
cmd='update resourcesavailable set available=available+1,issued=issued-1 where rid="{}"'.format(rid)
cursor.execute(cmd)
mydb.commit()
print('Resource Collected(Submitted)...')
def show_avail_res():
cmd='Select * from resourcesavailable;'
cursor.execute(cmd)
rec=cursor.fetchall()
l=['RID','Rname','Available','Issued']
for i in rec:
print({l[0]:i[0],l[1]:i[1],l[2]:i[2],l[3]:i[3]})
mydb.commit()
def add_resource():
rid=input('Enter RID : ')
res_name=input('Enter Resource Name : ')
add_qty=int(input('Enter Adding Quantity : '))
iss_qty=0
cmd="insert into ResourcesAvailable values ('8','Harmonium',10,0)"
cursor.execute(cmd)
mydb.commit()
print('Resource Added..')
def update_resource():
while True:
print('1. Reduce Quantity \n2. Increase Quantity \n3. Remove Old Resources \n4. Any Other Key To Back Menu')
select=input('Enter Your Choice : ')
if select=='1':
rid=input('Enter RID To Reduction : ')
n=int(input('Enter The Reduction Quantity : '))
cmd='Select * from resourcesavailable where rid="{}"'.format(rid)
cursor.execute(cmd)
rec=cursor.fetchall()
flag=False
for i in range(n):
if rec[0][2]==0:
print('Reduction Denied : Available Quantity Reached 0')
break
else:
cmd='update resourcesavailable set available=available-1 where rid="{}"'.format(rid)
cursor.execute(cmd)
mydb.commit()
flag=True
if flag==True:
print('Quantity Reduced')
elif select=='2':
rid=input('Enter RID For Increment : ')
n=int(input('Enter Increment Quantity : '))
for i in range(n):
cmd='update resourcesavailable set available=available+1 where rid="{}"'.format(rid)
cursor.execute(cmd)
mydb.commit()
print('Quantity Increased')
elif select=='3':
rid=input('Enter RID To Delete Resources : ')
cmd='delete from resourcesavailable where rid="{}"'.format(rid)
cursor.execute(cmd)
mydb.commit()
print('Record Of old Resource Is Deleted')
else:
break
def learners():
while True:
print('1. Add New Learner\n2. Show Learners\n3. Update Learner\n4. Delete Learner\n5. Key To Back Menu')
choose=int(input('Enter Your Choice : '))
if choose==1:
add_learner()
elif choose==2:
show_learners()
elif choose==3:
update_learner()
elif choose==4:
delete_learner()
elif choose==5:
break
else:
print('Choose Valid Option')
def staff():
while True:
print('1. Add New Staff\n2. Show Staff\n3. Update Staff\n4. Delete Staff\n5. To Back Menu')
choose=int(input('Enter Your Choice : '))
if choose==1:
add_staff()
elif choose==2:
show_staff()
elif choose==3:
update_staff()
elif choose==4:
delete_staff()
elif choose==5:
break
else:
print('Choose Valid Option')
def resources_issued():
while True:
print('1. Issue Resource\n2. Show Given Resources\n3. Collect Resource Issued\nAny Other Numeric Key To Back Menu')
choose=int(input('Enter Your Choice : '))
if choose==1:
issue_new()
elif choose==2:
show_given_resources()
elif choose==3:
resources_return()
else:
break
def resources_avail():
while True:
print('Press \n1. For Add New Resource\n2. For Update Quantity Of Resource\n3. For Show Available Resources\nAny Other Key To Back Menu')
select=input('Enter Your Choice : ')
if select=='1':
add_resource()
elif select=='2':
update_resource()
elif select=='3':
show_avail_res()
else:
break
def tables():
while True:
print('Press Key For Work On Table\n1. Learners\n2. Staff\n3. Resources Issued \n4. Resources Available \nAny Other Key To Back Menu')
select=input('Enter Your Choice : ')
if select=='1':
learners()
elif select=='2':
staff()
elif select=='3':
resources_issued()
elif select=='4':
resources_avail()
else:
break
def caller():
while True:
print('\n1. Show Tables Inside Database\n2. Show Schema Of Tables\n3. Work On Tables\n4. To Exit.')
ask=input('\nEnter Your Choice : ')
if ask=='1':
show_tables()
elif ask=='2':
table_schema()
elif ask=='3':
tables()
elif ask=='4':
break
else:
print('Choose Valid Option')
caller()
mydb.close()