27
27
from delphi .utils .epidate import EpiDate
28
28
import delphi .utils .epiweek as flu
29
29
from delphi .utils .geo .locations import Locations
30
+ from delphi_utils import get_structured_logger
31
+ from delphi .epidata .acquisition .rvdss .constants import LOGGER_FILENAME
32
+
30
33
31
34
32
35
respiratory_detections_cols = (
@@ -139,12 +142,19 @@ def get_num_rows(cursor, table_name):
139
142
pass
140
143
return num
141
144
142
- def update (data_dict ):
145
+ def update (data_dict , logger ):
143
146
# connect to the database
144
147
u , p = secrets .db .epi
145
148
cnx = mysql .connector .connect (user = u , password = p , database = "epidata" )
146
149
cur = cnx .cursor ()
147
-
150
+
151
+ # add filename for logger and exceptions
152
+
153
+ logger = get_structured_logger (
154
+ __name__ ,
155
+ filename = LOGGER_FILENAME ,
156
+ log_exceptions = True ,
157
+ )
148
158
149
159
for tt in data_dict .keys ():
150
160
data = data_dict [tt ]
@@ -153,18 +163,16 @@ def update(data_dict):
153
163
table_name = expected_table_names [tt ]
154
164
cols = expected_columns [tt ]
155
165
place_holders = ', ' .join (["?" for _ in cols ])
156
- # field_names = ", ".join(
157
- # f"`{name}`" for name in cols)
166
+ field_names = ", " .join (
167
+ f"`{ name } `" for name in cols )
158
168
159
- # check rvdss for new and/or revised data
160
- # sql = f"""
161
- # INSERT INTO {table_name} ({field_names})
162
- # VALUES ({place_holders})
163
- # """
169
+ field_values = ", " .join (
170
+ f"%({ name } )s" for name in cols )
164
171
172
+ #check rvdss for new and/or revised data
165
173
sql = f"""
166
- INSERT INTO { table_name }
167
- VALUES ({ place_holders } )
174
+ INSERT INTO { table_name } ( { field_names } )
175
+ VALUES ({ field_values } )
168
176
"""
169
177
170
178
# keep track of how many rows were added
@@ -176,8 +184,7 @@ def update(data_dict):
176
184
177
185
# keep track of how many rows were added
178
186
rows_after = get_num_rows (cur ,table_name )
179
- print (f"Inserted { int (rows_after - rows_before )} /{ int (total_rows )} row(s)" )
180
-
187
+ logger .info (f"Inserted { int (rows_after - rows_before )} /{ int (total_rows )} row(s) into table { table_name } " )
181
188
182
189
# cleanup
183
190
cur .close ()
0 commit comments