簡體   English   中英

Python sqlite3 庫不能使用 URI,即使 sqlite3 版本應該能夠

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

我的機器上安裝了最新的 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/"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM