Compare commits

..

1 Commits

Author SHA1 Message Date
0xee 6ead6853df Add mqtt-collect script 2020-03-30 20:28:59 +02:00
1 changed files with 2 additions and 8 deletions

View File

@ -10,14 +10,6 @@ import datetime
def init_db(path, topics): def init_db(path, topics):
db = sqlite3.connect(path) db = sqlite3.connect(path)
c = db.cursor()
for topic in topics:
table = topic.replace('/', '_')
c.execute(
f'CREATE TABLE IF NOT EXISTS {table} (timestamp timestamp, value real)'
)
db.commit()
return db return db
@ -45,6 +37,8 @@ def main():
table = msg.topic.replace("/", "_") table = msg.topic.replace("/", "_")
ts = datetime.datetime.now() ts = datetime.datetime.now()
value = float(msg.payload) value = float(msg.payload)
c.execute(f'CREATE TABLE IF NOT EXISTS {table} '
'(timestamp timestamp, value real)')
c.execute(f'INSERT INTO {table} VALUES (?, ?)', (ts, value)) c.execute(f'INSERT INTO {table} VALUES (?, ?)', (ts, value))
c.execute(f'SELECT * from {table}') c.execute(f'SELECT * from {table}')
db.commit() db.commit()