简体   繁体   English

数据库初始化时出现Flask语法错误

[英]Flask syntax error on database initialization

If anyone saw my other question, I was able to fix the issue, but a new one arose within the same function that I can't figure out. 如果有人看到我的另一个问题,我可以解决此问题,但是在我无法确定的相同功能内出现了一个新问题。

This function seems syntactically correct to me: 这个函数在语法上对我来说似乎是正确的:

def init_db():
    """Initializes the database."""
    with app.app_context():
        db = get_db()
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()

But I am getting this error: 但我收到此错误:

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-2-ad60ea155641> in <module>()
----> 1 init_db()

/Users/andrew/code/pos_system/routes.py in init_db()
     31         db = get_db()
     32         with app.open_resource('schema.sql', mode='r') as f:
---> 33             db.cursor().executescript(f.read())
     34         db.commit()
     35

OperationalError: near ")": syntax error

I don't see an errors with the parentheses on line 33... 我没有在第33行的括号中看到错误...

Answer from @alecxe in the comments: @alecxe在评论中的回答:

The error is not in the python code - it is inside the schema.sql . 错误不在python代码中,而是在schema.sql内部。 You can tell because the exception type was OperationalError (which is not a built-in python exception), not SyntaxError 您可以分辨出异常类型是OperationalError (这不是内置的python异常),而不是SyntaxError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM