28 lines
779 B
Python
28 lines
779 B
Python
|
|
import mysql.connector
|
||
|
|
from mysql.connector import Error
|
||
|
|
from config.db_config import db_config
|
||
|
|
|
||
|
|
def get_db_connection():
|
||
|
|
try:
|
||
|
|
connection = mysql.connector.connect(**db_config)
|
||
|
|
return connection
|
||
|
|
except Error as e:
|
||
|
|
print(f"Error connecting to database: {e}")
|
||
|
|
return None
|
||
|
|
|
||
|
|
def execute_query(query, params=None):
|
||
|
|
connection = get_db_connection()
|
||
|
|
if connection is None:
|
||
|
|
return None
|
||
|
|
try:
|
||
|
|
cursor = connection.cursor(dictionary=True)
|
||
|
|
cursor.execute(query, params)
|
||
|
|
connection.commit()
|
||
|
|
return cursor
|
||
|
|
except Error as e:
|
||
|
|
print(f"Error executing query: {e}")
|
||
|
|
return None
|
||
|
|
finally:
|
||
|
|
if connection.is_connected():
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|