-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb_utils.py
More file actions
71 lines (56 loc) · 2.12 KB
/
db_utils.py
File metadata and controls
71 lines (56 loc) · 2.12 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Database connection utility for HSEFZ Digital Life project.
This module provides a centralized way to create database connections
using environment variables for configuration.
"""
import os
import mysql.connector
from dotenv import load_dotenv
def get_database_connection():
"""
Create and return a MySQL database connection using environment variables.
Environment variables used:
- DB_HOST: Database host (default: localhost)
- DB_PORT: Database port (default: 3306)
- DB_USER: Database user (default: root)
- DB_PASSWORD: Database password (required)
- DB_NAME: Database name (default: selection_users)
- DB_AUTH_PLUGIN: Authentication plugin (default: caching_sha2_password)
Returns:
mysql.connector.connection.MySQLConnection: Database connection object
Raises:
mysql.connector.Error: If connection fails
"""
# Load environment variables from .env file
load_dotenv()
connection_config = {
'host': os.getenv('DB_HOST', 'localhost'),
'port': int(os.getenv('DB_PORT', '3306')),
'user': os.getenv('DB_USER', 'root'),
'password': os.getenv('DB_PASSWORD'),
'database': os.getenv('DB_NAME', 'selection_users'),
'auth_plugin': os.getenv('DB_AUTH_PLUGIN', 'caching_sha2_password')
}
# Remove None values to use defaults
connection_config = {k: v for k, v in connection_config.items() if v is not None}
return mysql.connector.connect(**connection_config)
def get_database_cursor():
"""
Get a database connection and cursor.
Returns:
tuple: (connection, cursor) - Remember to close both when done
"""
connection = get_database_connection()
cursor = connection.cursor()
return connection, cursor
# Example usage:
if __name__ == "__main__":
try:
cnx, cursor = get_database_cursor()
cursor.execute("SELECT 1")
result = cursor.fetchone()
print(f"Database connection test successful: {result}")
cursor.close()
cnx.close()
except Exception as e:
print(f"Database connection test failed: {e}")