diff options
author | pks <pks@pks.rocks> | 2021-05-11 10:08:46 +0200 |
---|---|---|
committer | pks <pks@pks.rocks> | 2021-05-11 10:08:46 +0200 |
commit | 19f9b2352fc499b0fc5302ef9e076f6fe9c204a8 (patch) | |
tree | ea1a32ab77e180585db7bc40b9f3117e5e02728d | |
parent | ebfe9cad5ef406052518af6a0c29d069f2dc04db (diff) |
python3.5 compat, dynamic insert query
-rw-r--r-- | config/requirements.txt | 3 | ||||
-rwxr-xr-x | weather-logger-receiver | 13 |
2 files changed, 10 insertions, 6 deletions
diff --git a/config/requirements.txt b/config/requirements.txt index 752aa56..fad19f7 100644 --- a/config/requirements.txt +++ b/config/requirements.txt @@ -1,3 +1,4 @@ pyRFXtrx==0.26.1 pyserial==3.5 -PyYAML==5.4.1 +PyYAML>=5.0 +future-fstrings diff --git a/weather-logger-receiver b/weather-logger-receiver index 4332396..0b0219a 100755 --- a/weather-logger-receiver +++ b/weather-logger-receiver @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: future_fstrings -*- import logging import os @@ -37,12 +38,14 @@ def setup_serial(device_path): def add_data_to_db(data, handle, db_connection): db_cursor = db_connection.cursor() + columns = ["timestamp", "handle", "temperature"] + values = [int(time.time()), handle, data.values['Temperature'] + if "Humidity" in data.values: + columns.append("humidity") + values.append(data.values['Humidity']) db_cursor.execute(f"INSERT INTO weather \ - (timestamp, handle, temperature, humidity) \ - VALUES ( {int(time.time())}, \ - \"{handle}\", \ - {data.values['Temperature']}, \ - {data.values['Humidity']})") + ({','.join(columns)}) \ + VALUES ({','.join(values)})") db_connection.commit() |