-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
33 lines (26 loc) · 870 Bytes
/
db.py
File metadata and controls
33 lines (26 loc) · 870 Bytes
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
import sqlite3
from pathlib import Path
DB_PATH = Path("journal.db")
def dict_factory(cursor, row):
return {col[0]: row[idx] for idx, col in enumerate(cursor.description)}
def get_db():
con = sqlite3.connect(
DB_PATH, check_same_thread=False
) # what is it, why are we setting to False
con.row_factory = dict_factory
con.execute("PRAGMA journal_mode=WAL;")
con.execute("PRAGMA synchronous=NORMAL;")
con.execute("PRAGMA foreign_keys=ON;")
return con
def init_db():
con = get_db()
con.execute("""
CREATE TABLE IF NOT EXISTS entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
con.execute("CREATE INDEX IF NOT EXISTS idx_entries_content ON entries(content)")
con.commit()
con.close()