�
���hM � � � d dl Z d dlZd dlZd dlmZ d dlmZ d� Zdd�Ze dk( r% e� dZ
ee
� \ ZZ e
ee
ee� � yy) � N)�Error)�load_dotenvc
� � d}d} t j j t j d� t j d� t j d� t j d� t j dd� �� }|j � r�|j
� }|j | � | j � j � j d � rT|j � }||j f|�|j � |�"|j � r|j � S S S |j � |j df|�|j � |�"|j � r|j � S S S |�|j � |�"|j � r|j � yyy# t $ rN}t# d
|� �� Y d}~|�|j � |�"|j � r|j � yyyd}~ww xY w# |�|j � |�"|j � r|j � w w w xY w)a
Connects to a MySQL database and executes a query.
Parameters:
- query (str): SQL query to execute
Returns:
- For SELECT queries: List of tuples containing the results
- For other queries: Number of rows affected
- None if an error occurs
N�DB_HOST�DB_NAME�DB_USER�DB_PASSWORD�DB_PORTi� )�host�database�user�password�port�SELECTz!Error while connecting to MySQL: )NN)�mysql� connector�connect�os�getenv�is_connected�cursor�execute�strip�upper�
startswith�fetchall�description�close�commit�rowcountr �print)�query�
connectionr �result�es �,/mnt/c/Users/abbas/Documents/MCPServer/db.py�execute_mysql_queryr'