sqlite3.Connection' 对象没有属性 'enable_load_extension

Jor*_*des 5 python sqlite

在 python3.7.4 上加载 sqlite3 模块并请求 enable_load_extension 给出:

import sqlite3
conn=sqlite3.connect("./tests/data/ne_110m_admin_0_countries.sqlite")
conn.enable_load_extension(True)
Run Code Online (Sandbox Code Playgroud)

AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

我知道默认的 Ubuntusqlite3包是在停用 load_extension的情况下构建的。我遵循了这个准则:https : //charlesleifer.com/blog/compiling-sqlite-for-use-with-python-applications/

基本上,使用标志编译 sqlite3: -DSQLITE_ENABLE_LOAD_EXTENSION,在详细模式下使用pyenv和构建python 3.7.4我可以看到正在使用的加载扩展标志,也遵循上面的教程并在 pyenv 上重新安装 pysqlite3

在详细模式下运行 python:

>>> import sqlite3
# /home/jesus/.pyenv/versions/3.7.4/lib/python3.7/sqlite3/__pycache__/__init__.cpython-37.pyc matches /home/jesus/.pyenv/versions/3.7.4/lib/python3.7/sqlite3/__init__.py
Run Code Online (Sandbox Code Playgroud)

模块的路径是正确的。

使用 sqlite3 客户端:

jesus@earth:~/.pyenv/versions/3.7.4/bin$ sqlite3
SQLite version 3.31.0 2019-11-16 12:04:38
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT sqlite_compileoption_used('ENABLE_LOAD_EXTENSION');
1
Run Code Online (Sandbox Code Playgroud)

我看到 sqlite 是用正确的选项构建的

永远不会越少我继续有同样的错误: AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

通过 SQL更新请求,如果库是用加载扩展加载编译的,则回复是肯定的

cursor=conn.cursor() 
res=cursor.execute("SELECT sqlite_compileoption_used('ENABLE_LOAD_EXTENSION');") 
res.fetchall() [(1,)]
Run Code Online (Sandbox Code Playgroud)

我不知道我还能做些什么来调试问题。这发生在 pyenv 构建上

有小费吗??

小智 5

python 必须为 SQLite 和 python 选项安装增强的系统变量。这对我有用:

# Do the following in your shell
LDFLAGS="-L/usr/local/opt/sqlite/lib -L/usr/local/opt/zlib/lib" CPPFLAGS="-I/usr/local/opt/sqlite/include -I/usr/local/opt/zlib/include" PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions" pyenv install 3.7.6
Run Code Online (Sandbox Code Playgroud)