-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
37 lines (31 loc) · 1.07 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import aiosqlite
import datetime
import asyncio
from matplotlib.pyplot import show
DATABASE = 'detections.db'
async def init_db():
async with aiosqlite.connect(DATABASE) as db:
await db.execute('''
CREATE TABLE IF NOT EXISTS detections (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
number_of_humans INTEGER NOT NULL
)
''')
await db.commit()
async def log_detection(number_of_humans):
timestamp = datetime.datetime.now().isoformat()
async with aiosqlite.connect(DATABASE) as db:
await db.execute('''
INSERT INTO detections (timestamp, number_of_humans)
VALUES (?, ?)
''', (timestamp, number_of_humans))
await db.commit()
async def show_table():
async with aiosqlite.connect(DATABASE) as db:
async with db.execute('SELECT * FROM detections') as cursor:
rows = await cursor.fetchall()
for row in rows:
print(row)
if __name__ == '__main__':
asyncio.run(show_table())