-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
48 lines (38 loc) · 1.15 KB
/
Copy pathdatabase.py
File metadata and controls
48 lines (38 loc) · 1.15 KB
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
38
39
40
41
42
43
44
45
46
47
48
import sqlite3
DB_NAME = "filtration.db"
def init_db():
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS filtration_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
turbidity REAL,
pressure REAL,
flow_rate REAL,
flux REAL
)
''')
conn.commit()
conn.close()
def insert_data(timestamp, turbidity, pressure, flow_rate, flux):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''
INSERT INTO filtration_data (timestamp, turbidity, pressure, flow_rate, flux)
VALUES (?, ?, ?, ?, ?)
''', (timestamp, turbidity, pressure, flow_rate, flux))
conn.commit()
conn.close()
def fetch_latest(limit=50):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''
SELECT timestamp, turbidity, pressure, flow_rate, flux
FROM filtration_data
ORDER BY id DESC
LIMIT ?
''', (limit,))
data = c.fetchall()
conn.close()
return data[::-1]