Flask: Connect to sqlite
05:21 09 Feb 2015

So I'm new to Python and I'm currently experimenting with Flask. I've read numerous docs and blogs on how to just configure the connection to a simple database using flask but without any success. Can you guys help me?

I don't want anything fancy right now, just to get a connection working. Below is the code right now with a simple test-query that I want to get working.

The result of this is an ordinary 500 Internal Server Error.

app = Flask(__name__)

from flask import Flask
import sqlite3
from flask import g

@app.before_request
def before_request():
    g.db = sqlite3.connect("database.db")


@app.teardown_request
def teardown_request(exception):
    if hasattr(g, 'db'):
        g.db.close()


@app.route('/')
def hello_world():
    g.db.execute("INSERT INTO test VALUES 'TestValue'")
    g.db.commit()


if __name__ == '__main__':
    app.run()
python database sqlite flask