1

I've have searched through and tried every solution to this problem that I could find I am still having issues with creating the database in the flask tutorial:

http://flask.pocoo.org/docs/0.10/tutorial/dbinit/#tutorial-dbinit

The previous stackoverflow answers to this question centered around changing the path in DATABASE = ... or adding the actual flaskr.db file to the project file (although I am under the impression that sqlite3 should generate it automatically)

I'm using Cloud9 - which runs on ubuntu - so the original /tmp/flaskr.db path should work, yet it does not.

The contents of my project are:

/Flaskr
  /static
  /templates
  flaskr.db 
  flaskr.py
  schema.sql

The contents of flaskr.py:

# all the imports
import sqlite3
from contextlib import closing
from flask import Flask, request, session, g, redirect, url_for, \
 abort, render_template, flash

app = Flask(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

def connect_db():
    return sqlite3.connect(app.config['DATABASE'])

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()



if __name__ == '__main__':
    app.run()

For DATABASE = '' , I have also tried entering direct path:

DATABASE = '/home/ubuntu/workspace/flaskr/flaskr.db'

and (yes, I realize these are the same thing)

DATABASE = '/flaskr/flaskr.db'

I would also like to note that the flaskr.db file under the flaskr project was not originally there - but I added while I was troubleshooting to see if I could provide it's direct path.

The error I get is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "flaskr.py", line 28, in init_db
    with closing(connect_db()) as db:
  File "flaskr.py", line 25, in connect_db
    return sqlite3.connect(app.config['DATABASE'])
KeyError: 'DATABASE'

Again, I'm using Cloud9 runs on a Unix OS

2 Answers 2

3

The trouble is that Flask.config.from_envvar is not finding your settings, because you have them here in flaskr.py.

You can use the from_object configuration method, as given in the Flask tutorial, like this:

app.config.from_object(__name__)

which would find your capitalized variable names as given. Or, if you wanted to continue using the from_envvar config, which is advisable since it keeps your database credentials out of revision control, you can put those items into a .cfg file as documented here.:

Make a new file called settings.cfg:

DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

And remove those lines from flaskr.py. Then to start your app:

$ export FLASKR_SETTINGS=/path/to/settings.cfg
$ python flaskr.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader...
Sign up to request clarification or add additional context in comments.

Comments

0

You are setting a variable here:

DATABASE = '/tmp/flaskr.db'

...and then you are trying to read it from a different place:

sqlite3.connect(app.config['DATABASE'])

You can simply:

sqlite3.connect(DATABASE)

or:

sqlite3.connect('/tmp/flaskr.db')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.