-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_handler.py
145 lines (119 loc) · 4.02 KB
/
sql_handler.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
from os import system
from time import sleep
import mysql.connector as sql
from mysql.connector import errorcode, cursor, connection
import donor_reg, event_reg
import sort_by_order
connection_obj = None
cursor_obj = None
def add_event():
global connection_obj, cursor_obj
try:
new_event = event_reg.Donation.from_user()
new_event.input_successful_donation()
new_event.evaluate_event()
cursor_obj.execute("INSERT INTO Donations (" +
", ".join(event_reg.Donation.header) +
") VALUES (" +
", ".join(["%s"] * len(event_reg.Donation.header)) +
")",
new_event.to_lists())
connection_obj.commit()
print("New event stored successfully")
except event_reg.UserInterrupt as interruption:
print(interruption)
except sql.Error as err:
print(err)
sleep(3)
def add_donor():
global connection_obj, cursor_obj
add_donor = ("INSERT INTO Donors "
"(name,weight,gender,date_of_birth,last_donation,last_month_sickness,unique_identifier,expiration_of_id,blood_type,hemoglobin,email,mobil) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)")
donors_data = donor_reg.main()
if donors_data is not None:
try:
cursor_obj.execute(add_donor, donors_data)
connection_obj.commit()
print("New donor stored successfully")
except sql.Error as err:
print(err)
sleep(3)
def remove_event_at(index: int):
global connection_obj, cursor_obj
cursor_obj.execute("SELECT COUNT(*) FROM Donations WHERE id = %s", (index,))
ans = cursor_obj.fetchall()
if ans[0][0]:
cursor_obj.execute("DELETE FROM Donations WHERE id = %s", (index,))
connection_obj.commit()
else:
raise KeyError("Key '%d' not in database!" % index)
def remove_event():
while True:
s_id = input("Please enter the id of donation event: ")
if s_id == 'q':
print("Operation interrupted by User")
break
try:
p_id = int(s_id)
remove_event_at(p_id)
print("Donation deleted successfully")
break
except KeyError as err:
print(err)
except ValueError:
print("input cannot be parsed as integer!")
except sql.Error as err:
print(err)
print("Error code: %d" % err.errno)
finally:
sleep(3)
sleep(3)
def remove_donor():
global connection_obj, cursor_obj
while True:
id = input("Enter the personal ID of the donor you want to delete")
if id == 'q':
print("Operation interrupted by User")
break
try:
cursor_obj.execute("SELECT COUNT(*) FROM Donations WHERE id = %s", (id,))
ans = cursor_obj.fetchall()
if ans[0][0]:
cursor_obj.execute("DELETE FROM Donors WHERE unique_identifier = '%s'" % id)
connection_obj.commit()
print("Donor object removed successfully")
break
else:
print("Key '%s' not in database!" % id)
sleep(3)
except sql.Error as err:
print(err)
sleep(3)
sleep(3)
def list_events():
try:
sort_by_order.sorting_donation_by_order(cursor_obj=cursor_obj)
except sql.Error as err:
print(err)
sleep(3)
def list_donors():
try:
sort_by_order.sorting_donor_by_order(cursor_obj=cursor_obj)
except sql.Error as err:
print(err)
sleep(3)
def search_in_donors():
pass
def search_in_events():
pass
def modify():
pass
def initialize(connection_data: dict):
global connection_obj, cursor_obj
connection_obj = sql.connect(**connection_data)
cursor_obj = connection_obj.cursor()
print("connection established!!!")
def shutdown():
cursor_obj.close()
connection_obj.close()