-
Notifications
You must be signed in to change notification settings - Fork 4
/
database.py
51 lines (43 loc) · 1.66 KB
/
database.py
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
import MySQLdb
import logging
from typing import Any, Dict, List
# Setup logging
logger = logging.getLogger(__name__)
class DatabaseManager:
"""Manages connections and operations with a MySQL database."""
def __init__(self, host: str, user: str, password: str, db: str):
self.host = host
self.user = user
self.password = password
self.db = db
self.connection = self.connect()
def connect(self) -> MySQLdb.connections.Connection:
"""Establishes a connection to the database."""
try:
connection = MySQLdb.connect(
host=self.host,
user=self.user,
passwd=self.password,
db=self.db
)
logger.info("Database connection established")
return connection
except MySQLdb.Error as e:
logger.error(f"Error connecting to database: {e}")
raise
def execute_query(self, query: str, params: Dict[str, Any] = None) -> List[Dict[str, Any]]:
"""Executes a query and returns the results."""
with self.connection.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(query, params)
return cursor.fetchall()
def execute_update(self, query: str, params: Dict[str, Any] = None) -> None:
"""Executes an update query."""
with self.connection.cursor() as cursor:
cursor.execute(query, params)
self.connection.commit()
def close(self) -> None:
"""Closes the database connection."""
self.connection.close()
logger.info("Database connection closed")
def __del__(self):
self.close()