-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connector.py
More file actions
38 lines (32 loc) · 1.08 KB
/
db_connector.py
File metadata and controls
38 lines (32 loc) · 1.08 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
from mysql.connector import MySQLConnection, Error
from config import DB_CONFIG
def get_connection():
"""Create and return a database connection"""
try:
return MySQLConnection(**DB_CONFIG)
except Error as e:
print(f"Error connecting to database: {e}")
return None
def call_stored_procedure(procedure_name, args=None):
"""Call a stored procedure and return the results"""
if args is None:
args = []
connection = get_connection()
if not connection:
return []
cursor = connection.cursor()
try:
cursor.callproc(procedure_name, args)
result_sets = []
for result in cursor.stored_results():
columns = [col[0] for col in result.description]
data = result.fetchall()
result_sets.append((data, columns))
connection.commit()
return result_sets
except Error as e:
print(f"Error calling stored procedure {procedure_name}: {e}")
return []
finally:
cursor.close()
connection.close()