-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.py
49 lines (37 loc) · 1.03 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
38
39
40
41
42
43
44
45
46
47
48
49
import sqlite3
PATH_TO_DB = "race_condition.sqlite"
def create_table(enable_wal_mode=False):
with sqlite3.connect(PATH_TO_DB) as conn:
c = conn.cursor()
c.execute("""DROP TABLE IF EXISTS messages;""")
conn.commit()
c.execute(
"""
CREATE TABLE messages (
ts DATE DEFAULT (datetime('now','localtime')),
msg TEXT
);
"""
)
conn.commit()
if enable_wal_mode:
c.execute("""pragma journal_mode=wal;""")
conn.commit()
def insert_row(record):
with sqlite3.connect(PATH_TO_DB) as conn:
c = conn.cursor()
c.execute(
"""
INSERT INTO messages
(msg)
VALUES
(?);
""",
record,
)
conn.commit()
def row_count():
with sqlite3.connect(PATH_TO_DB) as conn:
c = conn.cursor()
res = c.execute("""select count(*) from messages;""")
return res.fetchone()[0]