'How do I return SQL table using Python API with Provided Parameter?

I am trying to call an API to serve data from a MSSQL database. If I input only the endpoint without parameter, it successfully shows the error of missing param. When I insert param with wrong/unavailable id, it will show result as {"data": []}. However, if I insert correct parameter into the ID, the error 500 occur. May I know where should I adjust so that I will be able to get the data from the table. Below is my codes for the endpoints.

from flask_restful import Resource
from flask import request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
import app_config


class Test(Resource):

    def get(self):


        id = request.args.get("id", None)

        UGengine = create_engine(f'{app_config.SQL_SERVER_STRING}')
        UGSession = scoped_session(sessionmaker(bind=UGengine))

        s = UGSession()

        if id:
            query_data = s.execute(f"SELECT * FROM [DBName].[TABLE]"
                                   f"WHERE [ID] = '{id}'")
        else:
            return {"message": "Missing required query param: id"}, 400

        query_result = query_data.fetchall()
        return {"data": query_result}

in App.Py

api.add_resource(Test, "/test")


Solution 1:[1]

Are you sure about your Database structure?

Does your Database have really the Name "DBName" and your Table has the Name "TABLE"?

[DBName].[TABLE]

Br

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christoph