Snapshot after adding station table update code, but hasn't been tested on real frames yet.

This commit is contained in:
W1CDN 2023-07-09 09:14:09 -05:00
parent ee75cccc68
commit 8d94794c90
2 changed files with 30 additions and 12 deletions

View File

@ -66,7 +66,18 @@ def main():
conn.execute(sql, list(a.values())) conn.execute(sql, list(a.values()))
# TODO update stations table here # 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() conn.commit()

View File

@ -10,20 +10,27 @@ def get_db_connection():
conn = get_db_connection() conn = get_db_connection()
# example https://stackoverflow.com/a/50718957/2152245 # Grab a random row from frames table and pretend it is new
query1 = "INSERT INTO players (user_name, age) \ cur = conn.cursor()
VALUES('steven', 32) \ cur.execute("SELECT [from], id, created_unix FROM frames ORDER BY RANDOM() LIMIT 1;")
ON CONFLICT(user_name) \ rows = cur.fetchall()
DO UPDATE SET age=excluded.age;" 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) \ # Build query
ON CONFLICT(id) \ # "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;" DO UPDATE SET count = count + 1;"
query3 = "INSERT INTO stations ('from', frames_id, last_heard_unix, count) \ # example https://stackoverflow.com/a/50718957/2152245
VALUES('KC9TZN-9', 4068, 1687623864, 1)" # 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.commit()
conn.close() conn.close()