Skip to content

Instantly share code, notes, and snippets.

@ekiara
Created November 27, 2013 13:59
Show Gist options
  • Save ekiara/7676136 to your computer and use it in GitHub Desktop.
Save ekiara/7676136 to your computer and use it in GitHub Desktop.
SQL Alchemy SQLITE relative path
#SQL Alchemy SQLITE relative path
# Relative path is the path 'raw' after the three initial slashses
e = create_engine('sqlite:///path/to/database.db')
e = create_engine('sqlite:///relative/path/here.db')
# Absolute path is a slash after the three initial slashses
e = create_engine('sqlite:////tmp/absolute_path_database.db')
@konflic
Copy link

konflic commented Nov 21, 2020

If you need a step back do 'sqlite:///../tmp/absolute_path_database.db'

@valhuber
Copy link

Had trouble with just a string, I had to do this:

db_dir = "../../database/db.sqlite"
print(f'os.path.abspath(db_dir): {str(os.path.abspath(db_dir))}')
SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.abspath(db_dir)  # works
# SQLALCHEMY_DATABASE_URI = "sqlite:///" + db_dir  # fails

@m-primo
Copy link

m-primo commented Nov 10, 2021

Thanks @valhuber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment