This commit is contained in:
2025-05-03 15:40:58 +02:00
parent 138474aa0b
commit e9dc7c1192
5 changed files with 117 additions and 23 deletions

View File

@@ -4,13 +4,20 @@ import mysql.connector
logger = logging.getLogger(__name__)
def connetti_db(cfg):
"""Establishes a connection to the MySQL database.
"""
Establishes a connection to a MySQL database.
Args:
cfg: The configuration object containing database connection details.
cfg: A configuration object containing database connection parameters.
It should have the following attributes:
- dbuser: The database username.
- dbpass: The database password.
- dbhost: The database host address.
- dbport: The database port number.
- dbname: The name of the database to connect to.
Returns:
A MySQL database connection object.
A MySQL connection object if the connection is successful, otherwise None.
"""
try:
conn = mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport, database=cfg.dbname)
@@ -18,6 +25,5 @@ def connetti_db(cfg):
logger.info("Connected")
return conn
except mysql.connector.Error as e:
logger.error(f'{e}')
exit(e.errno)
return None
logger.error(f"Database connection error: {e}")
raise # Re-raise the exception to be handled by the caller