This script is a Flask web application that acts as a simple API for executing SQL queries on an Azure Synapse SQL database. It exposes a single API endpoint (/query) that accepts POST requests with a JSON payload containing a 'query' key, and it returns the results of the executed SQL query in JSON format. The application uses the pyodbc library to connect to the Azure Synapse SQL database and the pandas library to convert query results into a JSON-friendly format. It's a basic example and should be extended with security measures and additional features for a production environment.
- Python 3.10 installed
- Virtual Environment (recommended)
-
Clone the repository:
git clone https://github.com/v-macrev/flaskapi-azureconnection.git
-
Navigate to the project directory:
cd flaskapi-azureconnection
-
Create and activate a virtual environment:
python -m venv apienv source apienv/bin/activate # For Unix/Linux # or .\apienv\Scripts\activate # For Windows
-
Install the required dependencies:
pip install -r requirements.txt
-
Start the Flask web application:
flask run
or
python main.py
-
Open your favorite API testing tool, such as Postman or use the provided
test.http
file. -
Send sample requests to the API to execute SQL queries on the Azure Synapse SQL database.
For your convenience, I've included a test.http
file that contains sample HTTP requests to test various endpoints of the API. You can use this file with an extension like REST Client in Visual Studio Code or other similar tools.
If you encounter any issues or have suggestions for improvement, feel free to open an issue or submit a pull request.
Happy coding!