在flask应用程序中无法创建sqlite3数据库?

时间:2016-05-23 22:07:14

标签: python flask sqlite sqlalchemy

我有以下脚本:

#!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

但是当我真正运行脚本时,我什么也没看到:

$ 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://'>

那么为什么我看到SQLAlchemy引擎被放在内存中?

我的权限也没有错。

1 个答案:

答案 0 :(得分:0)

您所做的就是创建一个名为SQLALCHEMY_DATABASE_URI的变量。您还没有做任何事情来告诉SQLAlchemy使用该值。

the docs所示,您需要将其传递给app.config

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