-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.py
More file actions
168 lines (152 loc) · 5.21 KB
/
db.py
File metadata and controls
168 lines (152 loc) · 5.21 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from datetime import date, timedelta
import pymysql
import os
from dotenv import load_dotenv
from pymysql.cursors import DictCursor
load_dotenv()
DB_PASSWORD = os.environ['DB_PASSWORD']
def get_connection():
return pymysql.connect(
host='localhost',
user='root',
password = DB_PASSWORD,
db='dementia_app',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
def register_user(username, password):
conn = get_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM users WHERE username = %s", (username,))
if cursor.fetchone():
return False
cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password))
conn.commit()
return True
finally:
conn.close()
def insert_user_image(user_id, image_path, scheduled_date, description):
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = """
INSERT INTO user_images (user_id, image_path, scheduled_date, description)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(sql, (user_id, image_path, scheduled_date, description))
conn.commit()
finally:
conn.close()
def get_today_image(user_id, today_date):
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = """
SELECT * FROM user_images
WHERE user_id = %s AND scheduled_date = %s
ORDER BY id LIMIT 1
"""
cursor.execute(sql, (user_id, today_date))
return cursor.fetchone()
finally:
conn.close()
def get_next_available_date(user_id):
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = """
SELECT MAX(scheduled_date) AS last_date
FROM user_images
WHERE user_id = %s
"""
cursor.execute(sql, (user_id,))
result = cursor.fetchone()
last_date = result["last_date"]
today = date.today()
if last_date is None or last_date < today:
return today
else:
return last_date + timedelta(days=1)
finally:
conn.close()
def get_fallback_image():
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = "SELECT * FROM fallback_images ORDER BY RAND() LIMIT 1"
cursor.execute(sql)
return cursor.fetchone()
finally:
conn.close()
def insert_chat_report(user_id, image_id, fallback_image_id, chat_summary, memo=""):
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = """
INSERT INTO chat_reports (user_id, image_id, fallback_image_id, chat_summary, memo)
VALUES (%s, %s, %s, %s, %s)
"""
cursor.execute(sql, (user_id, image_id, fallback_image_id, chat_summary, memo))
conn.commit()
print("✅ DB 저장 성공")
except Exception as e:
print("❌ DB 저장 실패:", e)
finally:
conn.close()
def get_video_by_category(category, index=None):
conn = get_connection()
cursor = conn.cursor(pymysql.cursors.DictCursor)
if index is not None:
cursor.execute(
"SELECT url FROM media_library WHERE category=%s ORDER BY id LIMIT 1 OFFSET %s",
(category, index)
)
else:
cursor.execute(
"SELECT url FROM media_library WHERE category=%s ORDER BY RAND() LIMIT 1",
(category,)
)
row = cursor.fetchone()
conn.close()
return row['url'] if row else None
def get_latest_chat_report(user_id):
conn = get_connection()
try:
with conn.cursor(pymysql.cursors.DictCursor) as cursor:
sql = """
SELECT report_date, chat_summary, memo
FROM chat_reports
WHERE user_id = %s
ORDER BY report_date DESC
LIMIT 1
"""
cursor.execute(sql, (user_id,))
return cursor.fetchone()
finally:
conn.close()
def insert_user_info(user_id, name, age, trigger_elements, gender, dementia_level):
conn = get_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM user_info WHERE user_id = %s", (user_id,))
if cursor.fetchone():
return False
sql = """
INSERT INTO user_info (user_id, name, age, trigger_elements, gender, dementia_level)
VALUES (%s, %s, %s, %s, %s, %s)
"""
cursor.execute(sql, (user_id, name, age, trigger_elements, gender, dementia_level))
conn.commit()
return True
finally:
conn.close()
def get_user_info(user_id):
conn = get_connection()
try:
with conn.cursor() as cursor:
sql = "SELECT name, age, trigger_elements, gender, dementia_level FROM user_info WHERE user_id = %s"
cursor.execute(sql, (user_id,))
return cursor.fetchone()
finally:
conn.close()