From 19f9b2352fc499b0fc5302ef9e076f6fe9c204a8 Mon Sep 17 00:00:00 2001 From: pks Date: Tue, 11 May 2021 10:08:46 +0200 Subject: python3.5 compat, dynamic insert query --- weather-logger-receiver | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'weather-logger-receiver') 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() -- cgit v1.2.3