简体   繁体   English

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

[英]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? 那么,为什么我看到SQLAlchemy引擎被放置在内存中?

my permissions doesn't seem wrong either. 我的权限似乎也没有错。

All you've done is created a variable called SQLALCHEMY_DATABASE_URI. 您所要做的就是创建一个名为SQLALCHEMY_DATABASE_URI的变量。 You haven't done anything to tell SQLAlchemy to use that value. 您没有做任何事情告诉SQLAlchemy使用该值。

As shown in the docs , you need to pass it to app.config : 文档所示,您需要将其传递给app.config

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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