-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
54 lines (44 loc) · 1.04 KB
/
init.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
import sqlite3
def init_db(db_name='db.sqlite3'):
"""
Initialize database with tables
user
- user_id (primary key) autoincrement
- line_id (unique)
- create_date
record
- user_id (foreign key)
- record_id (primary key) autoincrement
- date
- item
- cost
- category
- comment
- create_date
"""
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute('''
CREATE TABLE user (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
line_id TEXT UNIQUE,
create_date DATE DEFAULT CURRENT_TIMESTAMP
)
''')
c.execute('''
CREATE TABLE record (
user_id INTEGER,
record_id INTEGER PRIMARY KEY AUTOINCREMENT,
date DATE DEFAULT CURRENT_TIMESTAMP,
item TEXT,
cost INTEGER,
category TEXT,
comment TEXT,
create_date DATE DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES user(user_id)
)
''')
conn.commit()
conn.close()
if __name__ == '__main__':
init_db()