Last active
September 2, 2021 04:20
-
-
Save andrewdoss-bit/4b684bf9dffd16a80928c846d27e0f50 to your computer and use it in GitHub Desktop.
Insert record
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
import bitdotio | |
columns = ['datetime', 'location', 'sensor_id', 'pm_2_5', 'pm_10'] | |
record_list = [record[col] for col in columns] | |
bit = bitdotio.bitdotio(BITDOTIO_API_KEY) | |
# Replace the line below with your schema-qualified table name | |
qualified_table = '"air_quality_log_test/air_quality"."pm_measurements"' | |
def execute_sql(bitdotio, sql, params=None): | |
"""Run arbitrary sql with parameters on bit.io.""" | |
try: | |
conn = bitdotio.get_connection() | |
cur = conn.cursor() | |
cur.execute(sql, params) | |
cur.close() | |
conn.commit() | |
except Exception as e: | |
logger.exception('A query error occurred') | |
raise e | |
finally: | |
if conn is not None: | |
conn.close() | |
def insert_record(bitdotio, record, qualified_table): | |
"""Inserts a single sensor measurement record. | |
""" | |
sql = f'INSERT INTO {qualified_table} ' | |
sql += 'VALUES (' + ', '.join(['%s'] * len(record)) + ');' | |
execute_sql(bitdotio, sql, record) | |
insert_record(bit, record_list, qualified_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment