![](/img/trans.png)
[英]Connecting to sqlite3 database from Flask application and printing data on a webpage
[英]Cannot create sqlite3 database in flask application?
我有以下腳本:
#!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引擎被放置在內存中?
我的權限似乎也沒有錯。
您所要做的就是創建一個名為SQLALCHEMY_DATABASE_URI的變量。 您沒有做任何事情告訴SQLAlchemy使用該值。
如文檔所示,您需要將其傳遞給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.