19

我正在尝试连接到 windows 7 x64 机器上的 python 3.3 应用程序中的 sqlite-database 文件。为此,文档指出:

# 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')

我想使用绝对路径,windows-equivalent to 是sqlite:////absolute/path/to/foo.db什么?数据库存储在C:/Users/Username/AppData/Roaming/Appname/mydatabase.db.

任何帮助表示赞赏!

4

2 回答 2

34

在 Windows 上,这有点棘手,因为您必须转义反斜杠:

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

此外,由于 Windows 没有驱动器的概念,root而是使用驱动器,因此您必须使用 3 个斜杠指定绝对路径:

sqlite:///C:\\Users\\Username\\AppData\\Roaming\\Appname\\mydatabase.db
于 2013-10-09T02:51:09.813 回答
1

根据SQLAlchemy docs,URL 也可以定义如下。

  • 相对路径

(这也适用于 Windows)

engine = create_engine('sqlite:///foo.db')
  • 绝对路径
# Windows alternative using raw string
engine = create_engine(r'sqlite:///C:\path\to\foo.db')
于 2021-08-20T20:55:36.693 回答