-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
100 lines (92 loc) · 2.28 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import sqlite3
DB_NAME = 'database.db'
conn = sqlite3.connect(DB_NAME)
conn.cursor().execute(
'''
CREATE TABLE IF NOT EXISTS users
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
)
'''
)
conn.cursor().execute(
'''
CREATE TABLE IF NOT EXISTS task_status
(
id INTEGER PRIMARY KEY NOT NULL,
status TEXT NOT NULL
)
'''
)
conn.cursor().execute(
'''
INSERT OR IGNORE
INTO task_status
VALUES (1, "TO DO")
'''
)
conn.cursor().execute(
'''
INSERT OR IGNORE
INTO task_status
VALUES (2, "DOING")
'''
)
conn.cursor().execute(
'''
INSERT OR IGNORE
INTO task_status
VALUES (3, "DONE")
'''
)
conn.cursor().execute(
'''
CREATE TABLE IF NOT EXISTS task_types
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT UNIQUE NOT NULL
)
'''
)
conn.cursor().execute(
'''
CREATE TABLE IF NOT EXISTS tasks
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT UNIQUE NOT NULL,
content TEXT NOT NULL,
task_status_id INTEGER NOT NULL,
task_type_id INTEGER NOT NULL,
FOREIGN KEY(task_status_id) REFERENCES task_status(id),
FOREIGN KEY(task_type_id) REFERENCES task_types(id)
)
'''
)
conn.cursor().execute(
'''
CREATE TABLE IF NOT EXISTS user_tasks
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
task_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (task_id) REFERENCES tasks(id)
)
'''
)
conn.cursor().execute(
'''
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_tasks
ON user_tasks (user_id, task_id);
'''
)
conn.commit()
class DB:
def __enter__(self):
self.conn = sqlite3.connect(DB_NAME)
return self.conn.cursor()
def __exit__(self, type, value, traceback):
self.conn.commit()