通用数据插入函数
(table_name, data)
| 253 | |
| 254 | |
| 255 | def insert_data(table_name, data): |
| 256 | """通用数据插入函数""" |
| 257 | if not data: |
| 258 | logger.error(f"{table_name} data fetch failed") |
| 259 | return |
| 260 | cursor = None |
| 261 | try: |
| 262 | start = time.monotonic() |
| 263 | cursor = conn.cursor() |
| 264 | if "data" in data: |
| 265 | data = data["data"] |
| 266 | logger.info(f'Inserting "{table_name}"...') |
| 267 | cursor.execute( |
| 268 | f'INSERT INTO "{table_name}" (data, insert_time) VALUES (%s, %s)', |
| 269 | (json.dumps(data), int(time.time())) |
| 270 | ) |
| 271 | logger.info(f'"{table_name}" inserted in {time.monotonic() - start:.2f}s') |
| 272 | except Exception as err: |
| 273 | logger.exception(f"Error inserting into {table_name}: {err}") |
| 274 | finally: |
| 275 | if cursor: |
| 276 | cursor.close() |
| 277 | |
| 278 | |
| 279 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected