Spaces:
Running
Running
Upload db.py
Browse files
db.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import psycopg2
|
3 |
+
|
4 |
+
os.environ['DB_USER'] = 'kheops'
|
5 |
+
os.environ['DB_PASSWORD'] = '3Wq4se8ahy0do79xrputgz&'
|
6 |
+
os.environ['DB_HOST'] = '51.159.25.78'
|
7 |
+
os.environ['DB_NAME'] = 'agilix'
|
8 |
+
os.environ['DB_PORT'] = '19114'
|
9 |
+
|
10 |
+
db_name = os.environ['DB_NAME']
|
11 |
+
db_user = os.environ['DB_USER']
|
12 |
+
db_host = os.environ['DB_HOST']
|
13 |
+
db_password = os.environ['DB_PASSWORD']
|
14 |
+
db_port = os.environ['DB_PORT']
|
15 |
+
|
16 |
+
conn = psycopg2.connect(f"dbname={db_name} user={db_user} host={db_host} password={db_password} port={db_port}")
|
17 |
+
conn.autocommit = True
|
18 |
+
|
19 |
+
def insert_score(question, response, score, comment):
|
20 |
+
try:
|
21 |
+
conn = psycopg2.connect(f"dbname={db_name} user={db_user} host={db_host} password={db_password} port={db_port}")
|
22 |
+
conn.autocommit = True
|
23 |
+
cur = conn.cursor()
|
24 |
+
|
25 |
+
# Define the SQL query to insert a record into the score table
|
26 |
+
insert_query = """
|
27 |
+
INSERT INTO score (question, response, score, comment)
|
28 |
+
VALUES (%s, %s,%s, %s);
|
29 |
+
"""
|
30 |
+
|
31 |
+
# Data to be inserted (example data)
|
32 |
+
data_to_insert = (question, response, score, comment)
|
33 |
+
|
34 |
+
# Execute the query with the provided data
|
35 |
+
cur.execute(insert_query, data_to_insert)
|
36 |
+
conn.commit()
|
37 |
+
cur.close()
|
38 |
+
return 'feedback sent'
|
39 |
+
except:
|
40 |
+
return 'Error'
|
41 |
+
def show_table():
|
42 |
+
cur = conn.cursor()
|
43 |
+
|
44 |
+
# Define the SQL query to select all records from the score table
|
45 |
+
select_query = "SELECT * FROM score;"
|
46 |
+
|
47 |
+
# Execute the query
|
48 |
+
cur.execute(select_query)
|
49 |
+
|
50 |
+
# Fetch all the rows from the result of the query
|
51 |
+
rows = cur.fetchall()
|
52 |
+
|
53 |
+
# Print the rows
|
54 |
+
for row in rows:
|
55 |
+
print(row)
|
56 |
+
|
57 |
+
|
58 |
+
# insert_score(conn, 1, 'test')
|
59 |
+
# show_table(conn)
|
60 |
+
|