简体   繁体   English

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

[英]Python sqlite3 library can't use URIs, even though sqlite3 version should be able to

I have the most recent sqlite3 installed on my machine:我的机器上安装了最新的 sqlite3:

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

And, in python, the sqlite3 module is using this version of sqlite3:而且,在 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)

However, I cannot open a database file using URIs, even though that feature has been present in sqlite since version 3.7:但是,我无法使用 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

What is going on here?这里发生了什么? What have I done wrong?我做错了什么?

OK, I figured out what was going on by reading the source code.好的,我通过阅读源代码弄清楚了发生了什么。 When pyenv compiled my version of Python, the _sqlite module was compiled against the ridiculously old CentOS version of the "sqlite3.h" file.当 pyenv 编译我的 Python 版本时,_sqlite 模块是针对可笑的旧 CentOS 版本的“sqlite3.h”文件编译的。 Because of this, the Python module didn't have the SQLITE_OPEN_URI macro defined, which causes it give a hard-coded "URIs not supported" Python exception.因此,Python 模块没有定义 SQLITE_OPEN_URI 宏,这会导致它给出硬编码的“不支持 URI”的 Python 异常。

To get around this, I had to set the following environment variable:为了解决这个问题,我必须设置以下环境变量:

# 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/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM