Search code examples
pythondatabasesqliteflaskpycharm

SQlite cant acess my path file for my database in pycharm


Im learing to use sqlite in pycharm and for some reason i cant acess my database using a my path, im gettin this error: "sqlalchemy.exc.ArgumentError: Could not parse SQLAlchemy URL from string 'sqlite:tarefas.db'".

This is my Main file:

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)

First it worked removing the "app" from the db but now, i cant move foward without having the app in there. If there is any other infomation needed i can provide. Thanks in advance!!! (❁´◡`❁)


Solution

  • **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.**