Sparrowcide Sparrowcide - 6 months ago 31
Python Question

Cannot create sqlite3 database in flask application?

I have the following script:

#!flask/bin/python
import os

from flask import Flask, render_template, request, url_for
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask("hello")

basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.sqlite3')
print SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)

db.create_all()

print db


But then when I actually run the script, I am not seeing anything:

$ python db_create.py
sqlite:////home/ubuntu/Paw/paw/app.sqlite3
/home/ubuntu/.virtualenvs/paw-py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.')
<SQLAlchemy engine='sqlite://'>


So why am I seeing the SQLAlchemy engine being placed in memory?

my permissions doesn't seem wrong either.

Answer

All you've done is created a variable called SQLALCHEMY_DATABASE_URI. You haven't done anything to tell SQLAlchemy to use that value.

As shown in the docs, you need to pass it to app.config:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite3')
db = SQLAlchemy(app)
Comments