Python sqlite3 库无法使用 URI,即使 sqlite3 版本应该能够

标签 python sqlite

我的机器上安装了最新的 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

这是怎么回事?我做错了什么?

最佳答案

好吧,我通过阅读源代码弄清楚了发生了什么。当 pyenv 编译我的 Python 版本时,_sqlite 模块是针对旧得可笑的 CentOS 版本的“sqlite3.h”文件进行编译的。因此,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/"

关于Python sqlite3 库无法使用 URI,即使 sqlite3 版本应该能够,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54060514/

相关文章:

python - 这个 TensorFlow 示例实际上是如何更新权重来找到解决方案的

android - 关于在 Android 中以编程方式备份​​ Google Drive 上的 Sqlite 文件和照片的示例项目

Android:不处理带有选择 WHERE 子句的 SQLite 查询

Python + Tkinter : "AttributeError"

python - 将属性注入(inject) QML 组件

android - 如何在Android Room持久库中创建不迁移版本的表?

Statement prepare 中的 Sqlite pendingOperation

Java - 什么可以使这段代码运行得更快?

python - numpy.poly1d ,寻根优化,在 x 轴上移动多项式

python - Python 中的 focus_set() 在第二个窗口上不起作用