-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
53 lines (46 loc) · 1.45 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
import os
import psycopg2
from datetime import datetime
def init_db():
"""Initialize the database and create tables if needed"""
conn = None
cur = None
try:
conn = psycopg2.connect(os.environ['DATABASE_URL'])
cur = conn.cursor()
# Create feedback table
cur.execute('''
CREATE TABLE IF NOT EXISTS feedback (
id SERIAL PRIMARY KEY,
session_id TEXT,
search_term TEXT,
nps_score INTEGER,
email TEXT,
comments TEXT,
created_at TIMESTAMP
)
''')
conn.commit()
except Exception as e:
print(f"Database initialization error: {e}")
finally:
if cur is not None:
cur.close()
if conn is not None:
conn.close()
def save_feedback(session_id, search_term, nps_score=None, email=None, comments=None):
"""Save feedback to PostgreSQL database"""
try:
conn = psycopg2.connect(os.environ['DATABASE_URL'])
cur = conn.cursor()
cur.execute(
'''
INSERT INTO feedback (session_id, search_term, nps_score, email, comments, created_at)
VALUES (%s, %s, %s, %s, %s, %s)
''',
(session_id, search_term, nps_score, email, comments, datetime.now())
)
conn.commit()
finally:
cur.close()
conn.close()