SQL Send Query Post man Use FastApi

  To create a directory and save a video in a FastAPI application in Python, you can use. Here's an example:

python
import os
import pyodbc
import uvicorn as uvicorn
from fastapi import FastAPI, UploadFile, File, Header

app = FastAPI()

@app.post('/api/sqlquery')
def SQlQueryRun(sql_query: str):
# Replace with your own connection string
# print(sql_query)
connection_string = 'Driver={SQL Server};Server=DESKTOP-FD8825Q\SQLEXPRESS;Database=MEYEPRO;Trusted_Connection=yes;'
# connection_string = f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}"
cnxn = pyodbc.connect(connection_string)
cursor = cnxn.cursor()
cursor.execute(sql_query)
columns = [column[0] for column in cursor.description]
data = []
for row in cursor.fetchall():
data.append(dict(zip(columns, row)))
cursor.close()
cnxn.close()
return data

uvicorn.run(app, host="192.168.100.12",port=8000)

This is a Python script that creates a FastAPI application and exposes a POST endpoint /api/sqlquery that accepts a sql_query parameter as a string. The endpoint executes the SQL query on a SQL Server database using the pyodbc library, retrieves the result set as a list of dictionaries, and returns the result set as a JSON response.

The script imports the following libraries:

  • os: for miscellaneous operating system interfaces
  • uvicorn: for running the FastAPI application
  • FastAPI: for creating the FastAPI application
  • UploadFile, File, and Header: for handling file uploads in FastAPI
  • pyodbc: for connecting to SQL Server databases using Python

The FastAPI function is called to create a new instance of the FastAPI application, which is assigned to the app variable.

The @app.post('/api/sqlquery') decorator is used to create a route for the /api/sqlquery endpoint, which accepts a POST request. The sql_query parameter is a required string parameter that contains the SQL query to execute on the SQL Server database.

The connection_string variable contains the connection string for the SQL Server database, which includes the driver name, server name, database name, and authentication method. The pyodbc.connect() function is called with the connection string to establish a connection to the SQL Server database.

The cursor object is created using the cnxn.cursor() method, which is used to execute the SQL query on the SQL Server database. The cursor.description property is used to retrieve the column names of the result set.

The result set is retrieved using the cursor.fetchall() method, which returns a list of tuples. Each tuple represents a row in the result set. The dict(zip(columns, row)) function is called to convert each row to a dictionary with the column names as keys and the row values as values.

The cursor.close() and cnxn.close() methods are called to close the cursor and the database connection, respectively.

Finally, the result set is returned as a JSON response using the return statement.

The uvicorn.run() function is called to run the FastAPI application on a specified host and port number. In this case, the application is run on the IP address 192.168.100.12 and port 8000.

Comments