-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_conn.py
More file actions
42 lines (33 loc) · 1023 Bytes
/
db_conn.py
File metadata and controls
42 lines (33 loc) · 1023 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
38
39
40
41
42
import os
import mysql.connector
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
DB_HOST =os.getenv("DB_HOST")
DB_PORT =os.getenv("DB_PORT")
DB_USER =os.getenv("DB_USER")
DB_PASSWORD =os.getenv("DB_PASSWORD")
DB_NAME =os.getenv("DB_NAME")
print(DB_HOST)
# try:
# # Connect to MySQL
# connection = mysql.connector.connect(
# host=DB_HOST,
# port=DB_PORT,
# user=DB_USER,
# password=DB_PASSWORD
# )
# if connection.is_connected():
# print(f"✅ Connected to MySQL at {DB_HOST}:{DB_PORT}")
# cursor = connection.cursor()
# cursor.execute("SHOW DATABASES")
# print("\n📂 Databases:")
# for db in cursor.fetchall():
# print(f"- {db[0]}")
# except mysql.connector.Error as err:
# print(f"❌ Error: {err}")
# finally:
# if 'connection' in locals() and connection.is_connected():
# cursor.close()
# connection.close()
# print("\n🔌 MySQL connection closed.")