2016-05-23 46 views
-1

我有以下脚本:无法在瓶子应用程序中创建sqlite3数据库?

#!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的发动机?

我的权限似乎也不错。

回答

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) 
相关问题