【问题标题】:Python sqlite3 library can't use URIs, even though sqlite3 version should be able toPython sqlite3 库不能使用 URI,即使 sqlite3 版本应该能够
【发布时间】:2019-01-07 00:45:13
【问题描述】:

我的机器上安装了最新的 sqlite3:

$ sqlite3 --version
3.26.0 2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9

而且,在 python 中,sqlite3 模块正在使用这个版本的 sqlite3:

$ python
Python 3.4.9 (default, Jan  5 2019, 18:35:56)
[GCC 5.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3 as sq
>>> sq.sqlite_version_info
(3, 26, 0)
>>> sq.version_info
(2, 6, 0)

但是,我无法使用 URI 打开数据库文件,即使该功能自 3.7 版以来就已存在于 sqlite 中:

>>> import sqlite3 as sq
>>> c = sq.connect('file://test', uri=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.NotSupportedError: URIs not supported

这里发生了什么?我做错了什么?

【问题讨论】:

  • 简单的/test 工作吗?你有 URI 支持的链接吗?
  • 错误信息是直接从Python模块生成的,不是sqlite库。这是什么操作系统,你是如何安装 Python 的? URI 支持应该在 sqlite3 模块 since Python 3.4
  • 我在 bluehost 的 VPS 系统上运行,所以这是我的完整版本字符串(来自 /proc/version):Linux 版本 2.6.32-754.6.3.el6.x86_64 (mockbuild@x86- 01.bsys.centos.org) (gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) ) #1 SMP Tue Oct 9 17:27:49 UTC 2018 我已经安装了这个版本的 Python 使用pyenv,它又是使用 linuxbrew 包管理器安装的。我没有使用系统的 yum 包管理器,因为它的“最新”版本的 SQLite 是从 2009 年开始的。对我来说毫无意义的是 Python 库在抱怨,但它确实看到了新的 SQLite 版本。

标签: python sqlite


【解决方案1】:

好的,我通过阅读源代码弄清楚了发生了什么。当 pyenv 编译我的 Python 版本时,_sqlite 模块是针对“sqlite3.h”文件的可笑的旧 CentOS 版本编译的。因此,Python 模块没有定义 SQLITE_OPEN_URI 宏,这导致它给出了硬编码的“不支持 URI”Python 异常。

为了解决这个问题,我必须设置以下环境变量:

# This is to direct pyenv to the linuxbrew include and library directories, when building versions of Python
export PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/linuxbrew/.linuxbrew/lib/ LDFLAGS=-L/home/linuxbrew/.linuxbrew/lib/ CPPFLAGS=-I/home/linuxbrew/.linuxbrew/include/"

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多