[Python] Why is SQLite unable to open the database on Flask?

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    On a Flask project, I keep getting the following error when I try to create a database file.

    # Get the directory of the current file (this file)
    basedir = os.path.abspath(os.path.dirname(__file__))

    # Define the database URI using a relative path
    app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(basedir, "database.db")}'
    app.config['SECRET_KEY'] = 'your_secret_key'


    sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file (Background on this error at: https://sqlalche.me/e/20/e3q8)


    On one computer it runs smoothly but on another, I keep getting errors like this. (Same Python version, and requirements are all installed.)

    Not sure what the difference is, any suggestions are appreciated!

    I tried changing the permissions of the database file. I also tried using an absolute path but that didnt help. Even when the database does not exist, on one computer running the script it just creates a new one, but on the other one it doesn't work and gives the same error.

    Continue reading...

Compartilhe esta Página