I'm trying to hook an existing sqlite3 db to a dashboard I'm building and I've run into a problem that I can't figure out how to resolve. I've been working on this by trying to piece together things from the Flask docs and other sources, so feel free to call me out on anything in here that looks a little odd. It probably is, and I just don't know it :)
Code:
from __future__ import with_statement
from contextlib import closing
from flask import Flask, render_template, request, session, g, redirect, url_for, abort, flash
import sqlite3
#config
DATABASE = '~/home/aaron/Dropbox/coding/webapp2/tmp/test.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
app = Flask(__name__)
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE']) # LINE 17
@app.before_request
def before_request():
g.db = connect_db() # LINE 22
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.close()
# App seems to error out before app.route and if __name__=='__main__' block
# Everything in my app.route is commented out
Complete error:
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in call return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1356, in full_dispatch_request rv = self.preprocess_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1539, in preprocess_request rv = func() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 22, in before_request g.db = connect_db() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 17, in connect_db return sqlite3.connect(app.config['DATABASE']) OperationalError: unable to open database file
127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /?debugger=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /?debugger=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /?debugger=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /?debugger=yes&cmd=resource&f=source.png HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:49] "GET /favicon.ico HTTP/1.1" 500 -
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in call return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1356, in full_dispatch_request rv = self.preprocess_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1539, in preprocess_request rv = func() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 22, in before_request g.db = connect_db() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 17, in connect_db return sqlite3.connect(app.config['DATABASE']) OperationalError: unable to open database file
It seems that the problem is coming from this config line:
DATABASE = '~/home/aaron/Dropbox/coding/webapp2/tmp/test.db'
My questions:
1) Why is the OperationalError being thrown twice?
2) Why does each OperationalError call out lines 17 and 22 (commented in my code above), even though these are function definitions and and not function calls?
3) How do I resolve the error, given that this is a valid db with data at the path specified?
These are what I'm referencing:
http://flask.pocoo.org/docs/tutorial/dbcon/#tutorial-dbcon
http://flask.pocoo.org/docs/tutorial/views/#tutorial-views
http://flask.pocoo.org/docs/patterns/sqlite3/
I think that the problem is the ~
character (valid in shell but not in Python), so you will probably need to write the full absolute path. I am not using Flask but I suggest to set up PROJECT_ROOT constant in your settings and then use relative paths:
import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DATABASE = os.path.join(PROJECT_ROOT, 'tmp', 'test.db')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With