遵循 Flask 教程,运行 Win 7、Python 2.7.3、virtualenv,我被困在第 3 步:创建数据库http://flask.pocoo.org/docs/tutorial/dbinit/#tutorial-dbinit
这样的模式可以通过将 schema.sql 文件传递到 sqlite3 命令来创建,如下所示:
sqlite3 /tmp/flaskr.db < schema.sql
如何运行这个命令,因为 CMD <venv> 返回:
"sqlite3" 不是内部或外部命令、可运行程序或批处理文件。
这一步有必要吗?
文件夹项目,2 个文件 schema.sql 和 flaskr.py。
架构.sql
drop table if exists entries;
create table entries (
id integer primary key autoincrement,
title string not null,
text string not null
);
烧瓶器.py
# all the imports
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 our little application :)
app = Flask(__name__)
app.config.from_object(__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') as f:
db.cursor().executescript(f.read())
db.commit()
if __name__ == '__main__':
app.run()
< venv > python
>>> from flaskr import init_db
>>> init_db()
Trackeback <most recent call last>:
File "<stdin>", line 1, in <module>
File "flaskr.py", line 24, in init_db
with closing (connect_db()) as db:
File "flaskr.py", line 21, in connect_db
return sqlite3.connect(app.config['DATABASE'])
sqlite3.OperationalError: unable to open database.