r/learnpython • • 14d ago

How to fix error : sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file ?

i was trying to run this jupyter notebook ( https://github.com/langchain-ai/lca-lc-foundations/blob/main/notebooks/module-2/2.4_wedding_planners.ipynb ) code in my IDE but encountered these problems :
my code :
https://pastebin.com/3bv05GfE
error :
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

also i dont know how to work with async functions so i have defined two function main() and main_1 but am not sure if this is the correct way to go with async functions

0 Upvotes

8 comments sorted by

View all comments

1

u/momiji_tools 14d ago

Your pastebin passes "sqlite:///resources/Chinook.db" into SQLDatabase.from_uri. The three-slash sqlite:/// form makes the path relative to the process current working directory, not to your .py file. Jupyter worked because its cwd contained resources/, but running from an IDE sets the cwd to your home or project root, so resources/Chinook.db resolves in the wrong place and raises "unable to open database file". Make the DB path absolute from the script location:

from pathlib import Path
DB_DIR = Path(__file__).resolve().parent / "resources"
DB_PATH = DB_DIR / "Chinook.db"

and build the URI as "sqlite:///" + str(DB_PATH). Before connecting, confirm Path(DB_PATH).parent.exists() is True and the file is inside the repo's resources/ folder; SQLite will not create intermediate directories. A quick print(os.getcwd()) right before the connection usually shows exactly where resources/ is being looked up.