简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM