SQLAlchemy engine absolute path URL in windows

人盡茶涼 提交于 2020-04-29 06:17:19

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/19260067/sqlalchemy-engine-absolute-path-url-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!