This project is a simple CRUD (Create, Read, Update, Delete) application built using FastAPI, pyodbc, and SQL Server. The application allows you to manage user records in a SQL Server database.
-
Clone the repository:
git clone https://github.com/your-username/your-repository.git cd your-repository -
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install fastapi uvicorn pyodbc
-
Configure the database connection:
Update the
connection_stringinmain.pywith your SQL Server details.server = 'your_server' database = 'TestDB' username = 'your_username' password = 'your_password' connection_string = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password}'
-
Ensure your SQL Server database has the necessary table:
CREATE TABLE test ( EmpID INT PRIMARY KEY, Name NVARCHAR(100), Address NVARCHAR(100), Phone NVARCHAR(15) );
Start the FastAPI application using Uvicorn:
uvicorn main:app --reloadThis will start the application and make it available at http://127.0.0.1:8000.
- URL:
/users/ - Method:
POST - Request Body:
{ "empId": 1, "name": "John Doe", "address": "123 Main St", "phone": "123-456-7890" } - Response:
{ "message": "User created successfully" }
- URL:
/users/{id} - Method:
GET - Response:
{ "EmpId": 1, "Name": "John Doe", "Address": "123 Main St", "Phone": "123-456-7890" }
- URL:
/users/{id} - Method:
PUT - Request Body:
{ "name": "John Doe", "address": "123 Main St", "phone": "123-456-7890" } - Response:
{ "message": "User updated successfully" }
- URL:
/users/{id} - Method:
DELETE - Response:
{ "message": "User deleted successfully" }
This project is licensed under the MIT License.