将 Flask 应用程序与 sqlite 连接时出现问题

问题描述 投票:0回答:1

我正在使用 SQLAlchemy python 模块将 Flask 应用程序与 sqlite 数据库(sqlite 的数据库浏览器)连接,但我不知道如何连接它。 我克隆了一个repo。 请告诉我是否需要执行任何额外步骤或其他操作。我已将存储库中存在的 .db 文件导入到数据库浏览器,但卡在这里并收到此错误

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.image_file AS user_image_file, user.password AS user_password
FROM user
WHERE user.username = ?
LIMIT ? OFFSET ?]
[parameters: ('21509016', 1, 0)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

请帮我解决这个错误。你可以在 github 中查看 main.py 代码并帮助我,我迫切希望运行这个项目!!!

我尝试使用 Gemini 和 chatgpt 寻找解决方案,但它并没有放弃标记答案!

python database sqlite flask sqlite3-python
1个回答
0
投票

该错误表明您需要在运行应用程序之前创建数据库实例。

我可以修复该问题并创建新用户并通过在

run.py
文件中运行应用程序之前创建表来登录用户:

from Main import app, db

if __name__ == '__main__':
    with app.app_context():
        db.create_all()   
    app.run(debug=True)

演示:

免责声明:该项目看起来像一个玩具项目,到处都有很多错误(例如固定的数据集路径)。

© www.soinside.com 2019 - 2024. All rights reserved.