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!
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 .
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
According to SQLAlchemy docs, URL can be defined as follows as well.
(this worked in windows as well)
engine = create_engine('sqlite:///foo.db')
# Windows alternative using raw string
engine = create_engine(r'sqlite:///C:\path\to\foo.db')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With