-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
37 lines (34 loc) · 945 Bytes
/
db.py
File metadata and controls
37 lines (34 loc) · 945 Bytes
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
import os
import mysql.connector
from mysql.connector import Error
from dotenv import load_dotenv
# Load variables from .env into environment
load_dotenv()
# 🔗 Connect to the Railway MySQL DB
def get_connection():
return mysql.connector.connect(
host=os.getenv("DB_HOST"),
port=int(os.getenv("DB_PORT", 3306)),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
database=os.getenv("DB_NAME")
)
# 📦 Fetch all trips from the DB
def get_all_trips():
conn = None
cursor = None
try:
conn = get_connection()
cursor = conn.cursor(dictionary=True)
query = "SELECT * FROM trips"
cursor.execute(query)
trips = cursor.fetchall()
return trips
except Error as err:
print("❌ Database Error:", err)
return []
finally:
if cursor:
cursor.close()
if conn:
conn.close()