无法通过控制台创建sqlite3数据库,如Flask Web Development一书中所示

时间:2014-07-19 12:35:18

标签: python sqlite flask flask-sqlalchemy

我不确定问题是什么,所以我会尽可能多地提供信息,希望有人能够发现问题所在。

本书向我展示了如何设计SQLAlchemy将用于创建数据库的对象。然后从控制台我必须执行以下操作:

(venv) $ python hello.py shell
>>> from hello import db
>>> db.create_all()

这应该在我的应用程序目录中创建一个数据库文件,但它没有。来自hello import db的行正常工作。

>>> db
<SQLAlchemy engine='sqlite://'>

db.create_all()不返回任何错误,但是我的应用程序目录中未创建数据库文件。我想提请注意这一行(它是否正确?):

app.config['SQLALECHMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite') #Final value: 'sqlite:////home/nick/dev/flare/data.sqlite'

这是我的整个hello.py文件:

import os
from flask import Flask, render_template, session, url_for, redirect, flash
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from flask.ext.sqlalchemy import SQLAlchemy
from wtforms import StringField, SubmitField
from wtforms.validators import Required

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = 'random key'
app.config['SQLALECHMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite') #Final value: 'sqlite:////home/nick/dev/flare/data.sqlite'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64), unique = True)
    users = db.relationship('User', backref='role')

    def __repr__(self):
        return '<Role %r>' % self.name


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), unique = True, index = True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username

class NameForm(Form):
    name = StringField('What is your name', validators=[Required()])
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name')
        session['name'] = form.name.data
        form.name.data = ''
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name=name)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

if __name__ == "__main__":
    manager.run()

1 个答案:

答案 0 :(得分:3)

配置中有拼写错误:

app.config['SQLALECHMY_DATABASE_URI'] =\

那里的SQLALCHEMY拼写错误,应该是:

app.config['SQLALCHEMY_DATABASE_URI'] =\

因为拼写错误了,所以使用了默认的sqlite://,即内存数据库。