SQlite 无法访问 pycharm 中数据库的路径文件

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

我正在学习在 pycharm 中使用 sqlite,由于某种原因,我无法使用我的路径访问我的数据库,我收到此错误:“sqlalchemy.exc.ArgumentError:无法从字符串 'sqlite:tarefas.db' 解析 SQLAlchemy URL”。

这是我的主文件:

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:/ ../database/tarefas.db"
db = SQLAlchemy(app)
class Tarefa(db.Model):
    __tablename__ = "tarefas"
    id = db.Column(db.Integer, primary_key=True)
    conteúdo = db.Column(db.String(200))
    feita = db.Column(db.Boolean)
    with app.app_context():
        db.create_all()
        db.session.commit()
@app.route("/")
def home():
    return render_template("index.html")


if __name__ == "__main__":
    app.run(debug=True)

首先它可以从数据库中删除“应用程序”,但现在,如果没有该应用程序,我就无法继续前进。如果需要任何其他信息我可以提供。提前致谢!!! (❁´◡`❁)

python database sqlite flask pycharm
1个回答
0
投票
**moving this statement to the outside of the class should do the work I believe**
with app.app_context():
    db.create_all()
**Also for the URI part use this: 'sqlite:///database.db' since app.app_context() will create an dir in the current dir.**
© www.soinside.com 2019 - 2024. All rights reserved.