http://dormousehole.readthedocs.org/en/latest/tutorial/dbinit.html#tutorial-dbinit照着这个弄的。
初始化数据库出错,初学者不明白问题出在哪,请帮忙指点一下
代码如下:
>>> from flaskr import init_db
>>> init_db()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "flaskr.py", line 22, in init_db
with closing(connect_db()) as db:
File "flaskr.py", line 19, in connect_db
return sqlite3.connect(app.config['DATABASE'])
KeyError: 'DATABASE'
>>>
# flaskr.py:
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from contextlib import closing
# configuration
DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
#create out little application :)
app = Flask(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
if __name__ == '__main__':
app.run()