Flask OperationalError: cannot open database file using sqlite3

I am trying to connect an existing sqlite3 db to the panel that I am creating and I am having a problem and cannot figure out how to solve it. I am working on this trying to combine things from Flask docs and other sources, so feel free to call me on something here that looks a little weird. Perhaps this is so, and I just don’t know :)

the 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 

Full error:

Traceback (last last call): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, to 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 be fore_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: cannot open file Database

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 (last last call): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, to 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 be fore_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: cannot open file Database

The problem seems to come from this configuration line:

 DATABASE = '~/home/aaron/Dropbox/coding/webapp2/tmp/test.db' 

My questions:

1) Why is the OperationalError statement repeated twice?

2) Why does each OperationalError raise lines 17 and 22 (commented out in my code above), although these are function definitions and not function calls?

3) How to fix the error, given that this is a valid db with data on the specified path?

This is what I refer to:

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/

+8
python flask sqlite3
source share
2 answers

I think the problem is the ~ character (valid in the shell, but not in Python), so you probably need to write the complete absolute path. I do not use Flask, but I suggest setting the 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') 
+10
source share

This worked for me:

When you define the database at the beginning, do not just say app.database = 'example.db' , instead you must specify the path to the home directory with a double slash :
"ex : //var//www//foldername//example.db" if you are using linux and restart the server again.

0
source share

All Articles