title: Flask: Connect to sqlite
body: <p>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?</p>
  
  <p>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.</p>
  
  <p>The result of this is an ordinary 500 Internal Server Error.</p>
  
  <pre><code>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()
  </code></pre>