forked from ishantk/GW2022PD1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession16.py
172 lines (127 loc) · 4.61 KB
/
Session16.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
"""
Principle of OOPS
Crossing
name, latitude, longitude, status, personInCharge
HealthLogger
phone, weight, bp, sugar, logTimeStamp
VegetableSeller
name, phone, amount, logTimeStamp, description, frequency
create table HealthLogger(
id int primary key auto_increment,
phone varchar(10),
weight int,
bp_high int,
bp_low int,
sugar int,
log_time_stamp datetime
);
"""
import datetime
import mysql.connector as db
class DBHelper:
def __init__(self):
self.connection = db.connect(user='root', password='',
host='127.0.0.1',
database='gw2022pd1')
print("DB CONNECTED :)")
self.cursor = self.connection.cursor()
print("CURSOR CREATED :)")
def write(self, sql):
self.cursor.execute(sql)
self.connection.commit()
print("SQL QUERY EXECUTED :)")
def read(self, sql):
self.cursor.execute(sql)
rows = self.cursor.fetchall()
for row in rows:
print(row)
# print(row[1], row[2])
class HealthLogger:
db_helper = DBHelper()
def save_log(self):
self.id = None
self.phone = input("Enter Phone Number: ")
self.weight = int(input("Enter Weight: "))
self.bp_high = int(input("Enter BP High: "))
self.bp_low = int(input("Enter BP Low: "))
self.sugar = int(input("Enter Diabetes: "))
dt = str(datetime.datetime.today())
idx = dt.rindex(".")
self.log_time_stamp = dt[0: idx]
sql = "insert into HealthLogger values(null, '{phone}', " \
"{weight}, {bp_high}, {bp_low}, {sugar}, " \
"'{log_time_stamp}')".format_map(vars(self))
print(sql)
save = input("Do you wish to Save the Record (yes/no)")
if save == "yes":
self.db_helper.write(sql)
def update_log(self):
self.id = int(input("Enter Log ID: "))
self.phone = input("Enter Phone Number: ")
self.weight = int(input("Enter Weight: "))
self.bp_high = int(input("Enter BP High: "))
self.bp_low = int(input("Enter BP Low: "))
self.sugar = int(input("Enter Diabetes: "))
dt = str(datetime.datetime.today())
idx = dt.rindex(".")
self.log_time_stamp = dt[0: idx]
sql = "update HealthLogger set" \
" phone = '{phone}', " \
"weight = {weight}, bp_high = {bp_high}, bp_low = {bp_low}, " \
"sugar = {sugar}, log_time_stamp = '{log_time_stamp}' " \
"where id = {id}".format_map(
vars(self))
print(sql)
update = input("Do you wish to Update the Record (yes/no)")
if update == "yes":
self.db_helper.write(sql)
def delete_log(self):
self.id = int(input("Enter Log ID: "))
sql = "delete from HealthLogger where id = {id}".format_map(
vars(self))
print(sql)
delete = input("Do you wish to Delete the Record (yes/no)")
if delete == "yes":
self.db_helper.write(sql)
def get_all_logs(self):
sql = "select * from HealthLogger"
self.db_helper.read(sql)
def get_logs_by_phone(self, phone):
sql = "select * from HealthLogger where phone = {}".format(phone)
self.db_helper.read(sql)
print("~~~~~~~~~~~~~~~~~~~~")
print("WELCOME")
print("HEALTH LOGGER APP")
print("~~~~~~~~~~~~~~~~~~~~")
while True:
print("1: Insert a New Health Log")
print("2: Update an Existing Health Log")
print("3: Delete an Existing Health Log")
print("4: Get All Health Logs")
print("5: Get All Health Logs By Phone")
option = int(input("Enter Your Option: "))
if option == 1:
print("Create a New Health Log")
logger = HealthLogger()
logger.save_log()
elif option == 2:
print("Update Existing Health Log")
logger = HealthLogger()
logger.update_log()
elif option == 3:
print("Delete Existing Health Log")
logger = HealthLogger()
logger.delete_log()
elif option == 4:
print("Get All Health Logs")
logger = HealthLogger()
logger.get_all_logs()
elif option == 5:
print("Get All Health Logs by Phone")
logger = HealthLogger()
logger.get_logs_by_phone(input("Enter Phone Number: "))
else:
print("Invalid Option")
choice = input("Would You Like to add another Health Log... (yes/no)")
if choice == "no":
break