-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
81 lines (70 loc) · 2.01 KB
/
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
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
import sqlite3
class db:
def __init__(self) -> None:
self.conn = sqlite3.connect("data.sqlite", check_same_thread=False)
self.cursor = self.conn.cursor()
self.initDB()
def __del__(self):
self.conn.close()
def initDB(self):
self.cursor.execute(
"""
SELECT name FROM sqlite_master WHERE type='table' AND name='diary'
"""
)
if not self.cursor.fetchone():
self.createTable()
def createTable(self):
self.cursor.execute(
"""
CREATE TABLE diary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user INTEGER NOT NULL,
date TEXT NOT NULL,
emotion TEXT NOT NULL,
diary TEXT NOT NULL,
gpt TEXT NOT NULL,
image TEXT
)
"""
)
self.conn.commit()
def insert(self, user, date, emotion, diary, gpt):
self.cursor.execute(
"""
INSERT INTO diary (user, date, emotion, diary, gpt) VALUES (?, ?, ?, ?, ?)
""",
(user, date, emotion, diary, gpt),
)
self.conn.commit()
self.cursor.execute(
"""
SELECT id FROM diary WHERE user = ? AND date = ?
""",
(user, date),
)
return self.cursor.fetchone()[0]
def updateImage(self, id, image):
self.cursor.execute(
"""
UPDATE diary SET image = ? WHERE id = ?
""",
(image, id),
)
self.conn.commit()
def getAll(self, user):
self.cursor.execute(
"""
SELECT * FROM diary WHERE user = ? ORDER BY id DESC
""",
(user,),
)
return self.cursor.fetchall()
def get(self, id):
self.cursor.execute(
"""
SELECT * FROM diary WHERE id = ?
""",
(id,),
)
return self.cursor.fetchone()