我一直在研究Flask教程并且遇到困难。
为了运行应用程序,您必须通过Python shell导入并运行init_db()
函数来创建数据库。
应用程序工作正常,但现在当我运行它时(我确定我没有改变任何东西!),我收到以下错误:
>>> from flaskr import init_db
>>> init_db()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "flaskr.py", line 39, in init_db
db.cursor().executescript(f.read())
sqlite3.OperationalError: near "subject": syntax error
我已经多次复制并粘贴了原始文档代码,但它仍然会发出相同的错误。
36 def init_db():
37 with closing(connect_db()) as db:
38 with app.open_resource('schema.sql', mode='r') as f:
39 db.cursor().executescript(f.read())
40 db.commit()
答案 0 :(得分:1)
我认为你需要缩进它:
36 def init_db():
37 with closing(connect_db()) as db:
38 with app.open_resource('schema.sql', mode='r') as f:
39 db.cursor().executescript(f.read())
40 db.commit()
答案 1 :(得分:0)
我遇到了同样的问题,但我发现sqlite3对脚本文件有限制格式要求。对于教程中的示例,最常见的错误可能是对于表声明中的最后一项,最后不再允许逗号(我想有些人可能希望在将来添加一个以便于扩展)。希望这有帮助〜