通用数据插入函数
(table_name, data)
| 267 | |
| 268 | |
| 269 | def insert_data(table_name, data): |
| 270 | """通用数据插入函数""" |
| 271 | if not data: |
| 272 | logger.error(f"{table_name} data fetch failed") |
| 273 | return |
| 274 | cursor = None |
| 275 | try: |
| 276 | start = time.monotonic() |
| 277 | cursor = conn.cursor() |
| 278 | if "data" in data: |
| 279 | data = data["data"] |
| 280 | logger.info(f'Inserting "{table_name}"...') |
| 281 | cursor.execute( |
| 282 | f'INSERT INTO "{table_name}" (data, insert_time) VALUES (%s, %s)', |
| 283 | (json.dumps(data), int(time.time())) |
| 284 | ) |
| 285 | logger.info(f'"{table_name}" inserted in {time.monotonic() - start:.2f}s') |
| 286 | except Exception as err: |
| 287 | logger.exception(f"Error inserting into {table_name}: {err}") |
| 288 | finally: |
| 289 | if cursor: |
| 290 | cursor.close() |
| 291 | |
| 292 | |
| 293 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected