教程地址:
http://docs.jinkan.org/docs/flask/tutorial/dbinit.html#tutorial-dbinit 2 from contextlib import closing
3 import sqlite3
4 from flask import Flask, request, session, g, redirect, url_for, \
5 abort, render_template, flash
6 # configuration
7 DATABASE = '/tmp/flaskr.db'
8 DEBUG = True
9 SECRET_KEY = 'development key'
10 USERNAME = 'admin'
11 PASSWORD = 'default'
12 # create our little application :)
13 app = Flask(__name__)
14 app.config.from_envvar('FLASKR_SETTINGS', silent=True)
15 def connect_db():
16 return sqlite3.connect(app.config['DATABASE'])
17 def init_db():
18 with closing(connect_db()) as db:
19 with app.open_resource('schema.sql') as f:
20 db.cursor().executescript(f.read())
21 db.commit()
22 if __name__ == '__main__':
23
app.run()
-----------------------------
报错:
>>> from flaskr import init_db
>>> init_db()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "
flaskr.py", line 18, in init_db
with closing(connect_db()) as db:
File "
flaskr.py", line 16, in connect_db
return sqlite3.connect(app.config['DATABASE'])
KeyError: 'DATABASE'
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/110558
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.