Created
May 24, 2025 14:55
-
-
Save h3xagn/6889e959f39770afbbb3d13f26cb2c74 to your computer and use it in GitHub Desktop.
Bulk insert for new data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def WriteSQLToDB(timestamp: datetime, data_points: list) -> None: | |
| try: | |
| sqlEngine = create_engine( | |
| db_conn, | |
| pool_recycle=300, | |
| pool_pre_ping=True, | |
| connect_args={"connect_timeout": 20}, | |
| ) | |
| # Build a single SQL statement with multiple VALUES | |
| values_list = [] | |
| for point in data_points: | |
| tag_name = point["tag_name"] | |
| tag_value = point["tag_value"] | |
| values_list.append(f"('{timestamp}', '{tag_name}', {tag_value})") | |
| sql = f"INSERT INTO sensors_data (time, tagname, tagvalue) VALUES {', '.join(values_list)}" | |
| with sqlEngine.connect() as connection: | |
| connection.execute(text(sql)) | |
| connection.commit() | |
| print(f"-- DB: {len(values_list)} records inserted in bulk.") | |
| except Exception as e: | |
| print(f"** DB: Could not save to database. Error: {e}") | |
| finally: | |
| sqlEngine.dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment