-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.py
274 lines (205 loc) · 7.23 KB
/
delete.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
import subprocess as sp
import pymysql
import pymysql.cursors
import sys
colors_dict3 = {
"BLUE": "\033[1;34m",
"RED": "\033[1;31m",
"CYAN": "\033[1;36m",
"GREEN": "\033[0;32m",
"RESET": "\033[0;0m",
"BOLD": "\033[;1m",
"REVERSE": "\033[;7m",
"ERROR": "\033[;7m"+"\033[1;31m"
}
def decorate3(color_str):
# print("Decorated")
sys.stdout.write(colors_dict3[color_str])
def del_print(msg):
decorate3("GREEN")
print(msg)
decorate3("RESET")
def err_print_msg(msg):
decorate3("ERROR")
print(msg)
decorate3("RESET")
def get_deletion_equation(attr,key_attrs,type_str):
query_str = ""
dict_len = len(attr.keys())
for i in range(dict_len):
#i += 1
if attr[key_attrs[i]] == '':
return ""
query_str += "`"+key_attrs[i]+"`"+" = "
if type_str[i]=='1':
query_str+'''"'''
query_str +="'" +attr[key_attrs[i]]+"'"
if type_str[i]=='1':
query_str+'''"'''
if i != dict_len-1:
query_str+" AND "
return query_str
# DONE
def delete_aircraft(cur,con):
#print("Inside delete_aircraft func")
table_name="Aircraft"
attr={}
key_attrs=["registration_num"]
attr[key_attrs[0]]=input("Enter reg num of aircraft you wish to delete: ")
try:
ans=get_deletion_equation(attr,key_attrs,"0") # non-int - pass 1 else pass 0
if ans=="":
print("Key attribute value cannot be left empty")
print("Failed to delete from database")
input("Press any key to continue")
return
query_str = "DELETE FROM "+table_name+" WHERE "+ans+" ; "
cur.execute(query_str)
con.commit()
res_cnt=cur.rowcount
if res_cnt == 0:
del_print("No such aircraft exists")
############################################
else:
del_print("Deleted aircraft")
###########################################
input("Press any key to continue")
except Exception as e:
con.rollback()
err_print_msg("Failed to delete from database")
err_print_msg(">>>>>>>>>>>>>", e)
input("Press any key to continue")
return
query_str2 = 'UPDATE Airline, Aircraft SET num_aircrafts_owned = num_aircrafts_owned - 1 WHERE fk_to_airline_owner_airline_IATA_code = `IATA airline designators`'
try:
cur.execute(query_str2)
con.commit()
except Exception as e:
err_print_msg('Failed to decrement number of aricrafts owned by airline the database.')
con.rollback()
err_print_msg(e)
input('Press any key to continue.')
return
# Done
def delete_luggage(cur,con):
#print("Inside delete_luggage func")
table_name="luggage"
attr={}
key_attrs=["Baggage ID"]
attr[key_attrs[0]]=input("Enter Baggage ID of luggage you wish to delete: ")
try:
ans=get_deletion_equation(attr,key_attrs,"0") # non-int - pass 1 else pass 0
if ans=="":
print("Key attribute value cannot be left empty")
print("Failed to delete from database")
input("Press any key to continue")
return
query_str = "DELETE FROM "+table_name+" WHERE "+ans+" ; "
cur.execute(query_str)
con.commit()
res_cnt=cur.rowcount
if res_cnt == 0:
del_print("No such luggage exists")
############################################
else:
del_print("Deleted luggage")
###########################################
input("Press any key to continue")
except Exception as e:
con.rollback()
err_print_msg("Failed to delete from database")
err_print_msg(">>>>>>>>>>>>>", e)
input("Press any key to continue")
return
#done
def delete_airline_crew(cur,con):
table_name="airline_crew"
attr={}
key_attrs=["Aadhar_card_number"]
attr[key_attrs[0]]=input("Enter Aadhar card number of airline employee you wish to delete: ")
try:
ans=get_deletion_equation(attr,key_attrs,"0") # non-int - pass 1 else pass 0
if ans=="":
print("Key attribute value cannot be left empty")
print("Failed to delete from database")
input("Press any key to continue")
return
query_str = "DELETE FROM "+table_name+" WHERE "+ans+" ; "
cur.execute(query_str)
con.commit()
res_cnt=cur.rowcount
if res_cnt == 0:
del_print("No such airline employee exists")
############################################
else:
del_print("Deleted airline employee")
###########################################
input("Press any key to continue")
except Exception as e:
con.rollback()
err_print_msg("Failed to delete from database")
err_print_msg(">>>>>>>>>>>>>", e)
input("Press any key to continue")
return
# DONE
def delete_airport_crew(cur,con):
#print("Inside delete_airport_crew func")
table_name="`Airport Employees/CREWS`"
attr={}
key_attrs=["Aadhar_card_number"]
attr[key_attrs[0]]=input("Enter Aadhar card number of airport employee you wish to delete: ")
try:
ans=get_deletion_equation(attr,key_attrs,"0") # non-int - pass 1 else pass 0
if ans=="":
print("Key attribute value cannot be left empty")
print("Failed to delete from database")
input("Press any key to continue")
return
query_str = "DELETE FROM "+table_name+" WHERE "+ans+" ; "
cur.execute(query_str)
con.commit()
res_cnt=cur.rowcount
if res_cnt == 0:
del_print("No such airport employee exists")
############################################
else:
del_print("Deleted airport employee")
###########################################
input("Press any key to continue")
except Exception as e:
con.rollback()
err_print_msg("Failed to delete from database")
err_print_msg(">>>>>>>>>>>>>", e)
input("Press any key to continue")
return
#done
def delete_route(cur,con):
#print("Inside delete_route func")
table_name="Route"
attr={}
key_attrs=["Route ID"]
attr[key_attrs[0]]=input("Enter route ID of route you wish to delete: ")
try:
ans=get_deletion_equation(attr,key_attrs,"0") # non-int - pass 1 else pass 0
if ans=="":
print("Key attribute value cannot be left empty")
print("Failed to delete from database")
input("Press any key to continue")
return
query_str = "DELETE FROM "+table_name+" WHERE "+ans+" ; "
cur.execute(query_str)
con.commit()
res_cnt=cur.rowcount
if res_cnt == 0:
del_print("No such route exists")
############################################
else:
del_print("Deleted route")
###########################################
input("Press any key to continue")
except Exception as e:
con.rollback()
err_print_msg("Failed to delete from database")
err_print_msg(">>>>>>>>>>>>>", e)
input("Press any key to continue")
return