Windows 中的 SQLAlchemy 引擎绝对路径 URL [英] SQLAlchemy engine absolute path URL in windows
问题描述
我正在尝试在 Windows 7 x64 机器上的 python 3.3 应用程序中连接到 sqlite 数据库文件.为此,文档指出:
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')
我想使用绝对路径,sqlite:////absolute/path/to/foo.db
的windows-equivalent是什么?数据库存储在C:/Users/Username/AppData/Roaming/Appname/mydatabase.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
.
感谢任何帮助!
推荐答案
在 Windows 上这有点棘手,因为您必须转义反斜杠:
On Windows it's a bit tricky, because you have to escape the backslashes:
sqlite:///C:\\path\\to\\database.db
此外,由于 Windows 没有 root
的概念,而是使用驱动器,因此您必须使用 3 个斜杠指定绝对路径:
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
这篇关于Windows 中的 SQLAlchemy 引擎绝对路径 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!