Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy engine absolute path URL in windows

I'm trying to connect to a sqlite-database file in a python 3.3 application on a windows 7 x64 machine. To do so, the documentation states:

# sqlite://<nohostname>/<path>
# where <path> is relative:
engine = create_engine('sqlite:///foo.db')

# or absolute, starting with a slash:
engine = create_engine('sqlite:////absolute/path/to/foo.db')

I would like to use the absolute path, what is the windows-equivalent to sqlite:////absolute/path/to/foo.db? The database is stored in C:/Users/Username/AppData/Roaming/Appname/mydatabase.db.

Any help is appreciated!

like image 771
zuiqo Avatar asked Oct 08 '13 23:10

zuiqo


People also ask

What is SQLAlchemy URL?

The typical form of a database URL is: dialect+driver://username:password@host:port/database. Dialect names include the identifying name of the SQLAlchemy dialect, a name such as sqlite , mysql , postgresql , oracle , or mssql .


2 Answers

On Windows it's a bit tricky, because you have to escape the backslashes:

sqlite:///C:\\path\\to\\database.db

Also, as Windows doesn't have the concept of root and instead uses drives, you have to specify absolute path with 3 slashes:

sqlite:///C:\\Users\\Username\\AppData\\Roaming\\Appname\\mydatabase.db
like image 169
plaes Avatar answered Sep 22 '22 09:09

plaes


According to SQLAlchemy docs, URL can be defined as follows as well.

  • Relative Path

(this worked in windows as well)

engine = create_engine('sqlite:///foo.db')
  • Absolute Path
# Windows alternative using raw string
engine = create_engine(r'sqlite:///C:\path\to\foo.db')
like image 28
Jayanga Jayathilake Avatar answered Sep 20 '22 09:09

Jayanga Jayathilake