-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite3.py
More file actions
47 lines (40 loc) · 1.46 KB
/
sqlite3.py
File metadata and controls
47 lines (40 loc) · 1.46 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
import sqlite3
import time
class RecordDAO:
def __init__(self, db_path, table_name):
self.db_path = db_path
self.table_name = table_name
self._create_table()
def _create_table(self):
with self.connect() as conn:
conn.execute(
f"CREATE TABLE IF NOT EXISTS {self.table_name} (name TEXT NOT NULL, status TEXT NOT NULL, time INT, PRIMARY KEY(name))"
)
def connect(self):
return sqlite3.connect(self.db_path)
def get_all_cfer(self):
with self.connect() as conn:
r = conn.execute(
f"SELECT name, status, time FROM {self.table_name}",
).fetchall()
return r
def get_cfer(self, name):
with self.connect() as conn:
r = conn.execute(
f"SELECT name, status, time FROM {self.table_name} WHERE name='{name}'",
).fetchone()
return r
def insert_cfer(self, name, msg):
now = time.time() // 1
with self.connect() as conn:
r = conn.execute(
f"REPLACE INTO {self.table_name}(name, status, time) VALUES('{name}', '{msg}', {now})",
).fetchall()
return r
def update_cfer(self, name, msg):
now = time.time() // 1
with self.connect() as conn:
r = conn.execute(
f"UPDATE {self.table_name} SET status='{msg}', time={now} WHERE name='{name}'",
).fetchall()
return r