Getting the RuntimeError: working outside of application context

I am trying to read and send the sensor value after connecting to the mqtt broker but when I query the database in mqtt callback function, I am getting the RuntimeError: working outside of application context How can I set up an application context with the app.app_context()?

import eventlet
import json
from flask import Flask, render_template
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from flask_bootstrap import Bootstrap
from Database import *


app = Flask(__name__)
app.config['SECRET'] = 'my secret key'
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['MQTT_BROKER_URL'] = brokerURL
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = ''
app.config['MQTT_PASSWORD'] = ''
app.config['MQTT_KEEPALIVE'] = 5
app.config['MQTT_TLS_ENABLED'] = False

mqtt = Mqtt(app)
socketio = SocketIO(app)
bootstrap = Bootstrap(app)

def handle_mqtt_message(client, userdata, message):
    dev = json.loads(message.payload.decode())
    socketio.emit('mqtt_message', dev)

def handle_connect(client, userdata, flags, rc):
    print("Connected to Broker!")
    sensor = Sensor.query.filter(Sensor.value == "1").first()
    if sensor is not None:
        print("Sensor value in mqtt: ", sensor.value)
        socketio.emit('value', sensor.value)

if __name__ == '__main__':, host=brokerURL, port=5000, use_reloader=False, debug=True)

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

brokerURL = ''
dbURL = "mysql://root:@localhost/test"

class Sensor(db.Model):
    __tablename__ = "sensor"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    value = db.Column(db.Integer, nullable=False)