-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (42 loc) · 1.47 KB
/
utils.py
File metadata and controls
50 lines (42 loc) · 1.47 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
import os
from dotenv import load_dotenv
import psycopg2
from psycopg2.extras import RealDictCursor
def run_sql(sql: str, params: tuple = ()):
# Load environment variables from .env
load_dotenv()
USER = os.getenv("user")
PASSWORD = os.getenv("password")
HOST = os.getenv("host")
PORT = os.getenv("port")
DBNAME = os.getenv("dbname")
try:
connection = psycopg2.connect(
user=USER,
password=PASSWORD,
host=HOST,
port=PORT,
dbname=DBNAME,
sslmode="require", # add SSL for Supabase
)
print("Connection successful!")
# Use a dict cursor so you get column names in results
with connection.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(sql, params)
# If the query returns rows (e.g. SELECT)
if cursor.description:
result = cursor.fetchall()
print(f"Returned {len(result)} rows")
return result
else:
# For INSERT/UPDATE/DELETE, commit and return nothing
connection.commit()
print("Query executed successfully (no return rows).")
return []
except Exception as e:
print(f"Failed to connect or run query: {e}")
return []
finally:
if 'connection' in locals() and not connection.closed:
connection.close()
print("Connection closed.")