Skip to main content
Glama

Data Engineer Agent

by abbasnosrat
db.cpython-312.pyc5.31 kB
� ���hM���ddlZddlZddlZddlmZddlmZd�Zdd�Ze dk(r%e�dZ ee �\Z Z e ee e e ��yy) �N)�Error)� load_dotenvc ��d}d} tjjtjd�tjd�tjd�tjd�tjdd���}|j �r�|j �}|j|�|j�j�jd �rT|j�}||jf|�|j�|�"|j �r|j�SSS|j�|jdf|�|j�|�"|j �r|j�SSS |�|j�|�"|j �r|j�yyy#t $rN}t#d |���Yd}~|�|j�|�"|j �r|j�y y y d}~wwxYw#|�|j�|�"|j �r|j�wwwxYw) 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's%���J� �F�#��_�_�,�,����9�%��Y�Y�y�)����9�%��Y�Y�}�-����9�d�+� -� � � � "� "� $��&�&�(�F� �N�N�5� !��{�{�}�"�"�$�/�/��9����*���v�1�1�1� � � �L�L�N� � !�j�&=�&=�&?� � � � �'@� !��!�!�#�����,� � � �L�L�N� � !�j�&=�&=�&?� � � � �'@� !�1 %�, � � �L�L�N� � !�j�&=�&=�&?� � � � �'@� !�� �� �1�!��5�6�� � � �L�L�N� � !�j�&=�&=�&?� � � � �'@� !���� � � �L�L�N� � !�j�&=�&=�&?� � � � �'@� !�s0�C?G�<G� H�H�H"�H�H"�"8Ic ��|j�j�j�d}||tjj �j �dd�}|dk(rn|s d|d<g|d<|S |D�cgc]}|d�� }}g} |D]&} | jtt|| ����(|j| t|�d d ��|S|d vr|j||�d �d ��|S|j|�d�dd��|Scc}w#t $r0tt|d��D�cgc]}d|���� ncc}w}}Y��wxYw)aa Formats MySQL query results for optimal LLM comprehension. Args: query (str): The SQL query that was executed results (list): The query results (for SELECT) rowcount (int): Number of rows affected (for INSERT/UPDATE/DELETE) Returns: dict: A structured response with metadata and formatted data r�success)r"�type� timestamp�statusrz3Query executed successfully but returned no results�message�results�column_z5Each result is a dictionary with column names as keys)r.�count�format)�INSERT�UPDATE�DELETEz! operation completed successfully)� rows_affectedr-z operation executed successfullyz(No results to return for this query type)r-�note) rr�split�datetime�now� isoformat�AttributeError�range�len�append�dict�zip�update) r"r.rr � query_type�response�desc�columns�i�formatted_results�rows r&�format_results_for_llmrI?s�������$�$�&�,�,�.�q�1�J����&�&�*�*�,�6�6�8�� �H��X���"W�H�Y� �"$�H�Y� ��O� F�+6�7�4�t�A�w�7�G�7� ��� >�C� � $� $�T�#�g�s�*;�%<� =� >� ���(���\�M� � �$ �O� �5� 5����%�$��%F�G� � � �O� ���$��%E�F�>� � � �O��98��� F�.3�C��� �O�.D�E�����}�E��E�G�E� F�s0�5D�9 D �D� D�"E�1 D>�=E�E�__main__z�INSERT INTO cars (brand, model, color, `condition`, mileage, price, owner_email) VALUES ('Toyota', 'Corola', 'Silver', 'used', 45000, 18500.00, 'john.dee@example.com'),)N)r8r�mysql.connectorrr�dotenvrr'rI�__name__r"r.rr!��r&�<module>rPs_��� ��!��2�n9�z �z���M�  X�E�/�u�5��G�[� � ��w� � <�=�rO

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/abbasnosrat/MCPServerAgent'

If you have feedback or need assistance with the MCP directory API, please join our Discord server