这是我的 flaskr.py 的一部分, 我试图在 127.0.0.1:5000 上运行它,但它给了我一个内部错误。 然后我检查了 error_log,它告诉我,
File "/var/www/html/flaskr/flaskr.py", line 66, in show_entries (model,sn,user,status))
OperationalError: no such table: entries "
这是我的代码:
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def init_db():
"""Creates the database."""
with app.app_context():
db = get_db()
with app.open_resource('/var/www/html/flaskr/schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
def get_db():
"""
Opens a new database connection if there is none yet for the
current application context.
"""
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
sqlite_db = sqlite3.connect(app.config['DATABASE'])
sqlite_db.row_factory = sqlite3.Row
top.sqlite_db = sqlite_db
return top.sqlite_db
@app.route('/')
def show_entries():
db = get_db()
with open('/var/www/html/flaskr/hardwarelist.txt') as fl:
for eachline in fl:
(model,sn,user,status)=eachline.strip().split(',')
db.execute('insert into entries (model,sn,user,status) values (?, ?, ?, ?)',(model,sn,user,status))
fl.close()
cur = db.execute('select model,sn,status,user from entries order by id desc')
entries = cur.fetchall()
return render_template('show_entries.html', entries=entries)
if __name__ == '__main__':
init_db()
app.run()
我以为是我的“init_db”函数运行不正常导致的错误,所以我无法获取条目表。 但是这里没有关于 init_db 的错误报告,我应该怎么做才能弄清楚这个问题并解决它?
更新: 这是我的 schema.sql:
drop table if exists entries;
create table entries (
id integer primary key autoincrement,
model text not null,
sn text not null,
status text not null,
user text not null
);
请您参考如下方法:
在未正确创建 db 文件的相同情况下,对我有帮助的是从终端运行。
flask init-db
然后再次运行应用
flask run
这正确更新了示例数据库文件。
