From 8d94794c90557b2fb6ed607eeb2401e95bd51eac Mon Sep 17 00:00:00 2001 From: W1CDN Date: Sun, 9 Jul 2023 09:14:09 -0500 Subject: [PATCH] Snapshot after adding station table update code, but hasn't been tested on real frames yet. --- kiss_and_db.py | 13 ++++++++++++- test_db.py | 29 ++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/kiss_and_db.py b/kiss_and_db.py index 1bfae30..3cd25b1 100644 --- a/kiss_and_db.py +++ b/kiss_and_db.py @@ -66,7 +66,18 @@ def main(): conn.execute(sql, list(a.values())) # TODO update stations table here - + # Original intent was to include the id from the frames table, + # but that would mean making another conn.commit() (I think). + # It's not immediately needed, so I'm skipping it. + # Build query + # "from" is wrappedin [] because it is a reserved word and using '' doesn't work. + station_update = ", ".join((a['from'], a['created_unix'], "1")) + query3 = "INSERT INTO stations ([from], last_heard_unix, count) \ + VALUES("+station_update+", 1) \ + ON CONFLICT([from]) \ + DO UPDATE SET count = count + 1;" + # Insert/update data + conn.execute(query3) conn.commit() diff --git a/test_db.py b/test_db.py index 1dcdf8b..e0959e5 100644 --- a/test_db.py +++ b/test_db.py @@ -10,20 +10,27 @@ def get_db_connection(): conn = get_db_connection() -# example https://stackoverflow.com/a/50718957/2152245 -query1 = "INSERT INTO players (user_name, age) \ -VALUES('steven', 32) \ -ON CONFLICT(user_name) \ -DO UPDATE SET age=excluded.age;" +# Grab a random row from frames table and pretend it is new +cur = conn.cursor() +cur.execute("SELECT [from], id, created_unix FROM frames ORDER BY RANDOM() LIMIT 1;") +rows = cur.fetchall() +results = dict(rows[0]) +values = ', '.join('"%s"' % w for w in results.values()) -query2 = "INSERT INTO stations (id, 'from', frames_id, last_heard_unix, count) \ -VALUES(1, 'KC9TZN-9', 4068, 1687623864, 1) \ -ON CONFLICT(id) \ + +# Build query +# "from" is wrappedin [] because it is a reserved word and using '' doesn't work. +query3 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \ +VALUES("+values+", 1) \ +ON CONFLICT([from]) \ DO UPDATE SET count = count + 1;" -query3 = "INSERT INTO stations ('from', frames_id, last_heard_unix, count) \ -VALUES('KC9TZN-9', 4068, 1687623864, 1)" +# example https://stackoverflow.com/a/50718957/2152245 +# query2 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \ +# VALUES('KC9TZN-8', 4068, 1687623864, 1) \ +# ON CONFLICT([from]) \ +# DO UPDATE SET count = count + 1;" -conn.execute(query2) +conn.execute(query3) conn.commit() conn.close()