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

时间:2019-01-06 02:15:01

标签: 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)

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

>>> 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

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

1 个答案:

答案 0 :(得分: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/"