【问题标题】:SQLAlchemy engine absolute path URL in windowsWindows中的SQLAlchemy引擎绝对路径URL
【发布时间】:2013-10-16 02:32:30
【问题描述】:

我正在尝试连接到 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

感谢任何帮助!

【问题讨论】:

    标签: python sqlite sqlalchemy


    【解决方案1】:

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

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

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

    sqlite:///C:\\Users\\Username\\AppData\\Roaming\\Appname\\mydatabase.db
    

    【讨论】:

      【解决方案2】:

      根据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')
      

      【讨论】:

        猜你喜欢
        • 2014-07-31
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        • 2021-10-18
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        相关资源
        最近更新 更多