-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcreate_db.py
44 lines (35 loc) · 994 Bytes
/
create_db.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
import sys
import sqlite3
CREATE_CHATS_TABLE_SQL = """
CREATE TABLE chats (
id VARCHAR(128) PRIMARY KEY,
created_at TEXT,
frequency VARCHAR(16),
language VARCHAR(16))
"""
CREATE_QUIZES_TABLE_SQL = """
CREATE TABLE quizes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id VARCHAR(128),
created_at TEXT,
type VARCHAR(10),
question_number INT,
FOREIGN KEY (chat_id) REFERENCES chats (id)
ON DELETE CASCADE ON UPDATE NO ACTION)
"""
CREATE_ANSWERS_TABLE_SQL = """
CREATE TABLE answers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
quiz_id UNSIGNED BIG INT,
question_number INT,
answer INT,
FOREIGN KEY (quiz_id) REFERENCES quizes (id)
ON DELETE CASCADE ON UPDATE NO ACTION)
"""
def create_database(db_name):
conn = sqlite3.connect(db_name)
conn.execute(CREATE_CHATS_TABLE_SQL)
conn.execute(CREATE_QUIZES_TABLE_SQL)
conn.execute(CREATE_ANSWERS_TABLE_SQL)
if __name__ == '__main__':
create_database(sys.argv[1])