Skip to content Skip to sidebar Skip to footer

Creating A Sqlite Database Connection For A Flask App

I am working on a web back-end which reads from a database file, processes the data and returns a json object. I am not really informed about flask and the way the variables life i

Solution 1:

You can do it like this:

#file flaskApp.py#!/usr/bin/env python3.6from flask import Flask
...


app = Flask(__name__)

g.db = sqlite3.connect("database.db")
cursor = get_db().cursor()

@app.route('/getResourceUsage/<string:buildingNumber>')defgetResource(buildingNumber):
    cursor.execute("SELECT * FROM room WHERE building='" + buildingNumber + "'")
    ...
    return json

Now you can access cursor to make queries in all endpoints

Post a Comment for "Creating A Sqlite Database Connection For A Flask App"