如何在 Python 3.7 中使用带有 sqlite3 python 模块的 FTS5 扩展? [英] How can I use the FTS5 extension with the sqlite3 python module with Python 3.7?
问题描述
如何在 Python 3.7 中使用 FTS5
扩展和 sqlite3
python 模块?
How can I use the FTS5
extension with the sqlite3
python module with Python 3.7?
我尝试使用 python testFTS5.py
在 Python 中运行以下代码:
I tried to run the following code in Python with python testFTS5.py
:
import sqlite3
conn = sqlite.connect('some_db.db')
sqlite.enable_load_extension(True)
sqlite.load_extension('fts5')
导致错误消息:
Traceback (most recent call last):
File "./src/test.py", line 3, in <module>
sqlite.enable_load_extension(True)
AttributeError: module 'sqlite3' has no attribute 'enable_load_extension'
我尝试了 sqlite.load_extension('FTS5')
和 sqlite.load_extension('ENABLE_FTS5')
但不出所料地产生了相同的错误消息(相应的文件名是未找到).我还尝试使用 LD_LIBRARY_PATH=/usr/local/bin python testFTS5.py
运行代码,但我收到相同的错误消息.
I tried sqlite.load_extension('FTS5')
and sqlite.load_extension('ENABLE_FTS5')
but it unsurprisingly yields the same error message (with the corresponding filename being not found). I also tried running the code with LD_LIBRARY_PATH=/usr/local/bin python testFTS5.py
but I get the same error message.
我通过在终端中运行以下代码检查了 sqlite3
位置:
I checked the sqlite3
location by running the following code in the terminal:
derno@ompn:/mnt/ilcompn0d1/user/dernonco/fts-test$ which sqlite3
/usr/local/bin/sqlite3
并且我列出了已安装的 sqlite3
扩展:
and I listed the installed sqlite3
extensions:
derno@ompn:/mnt/ilcompn0d1/user/dernonco/fts-test$ sqlite3
SQLite version 3.18.0 2017-03-28 18:48:43
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> pragma compile_options;
COMPILER=gcc-5.4.0 20160609
DEFAULT_SYNCHRONOUS=2
DEFAULT_WAL_SYNCHRONOUS=2
ENABLE_FTS5
ENABLE_RTREE
SYSTEM_MALLOC
THREADSAFE=1
这似乎表明 FTS5 在我的 /usr/local/bin/sqlite3
版本中可用.
which seems to indicate that the FTS5 is available in my /usr/local/bin/sqlite3
version.
但是当我跑步时
import sqlite3
con = sqlite3.connect(':memory:')
cur = con.cursor()
cur.execute('pragma compile_options;')
available_pragmas = cur.fetchall()
con.close()
print(available_pragmas)
它输出:
[('COMPILER=gcc-5.4.0 20160609',), ('DEFAULT_SYNCHRONOUS=2',), ('DEFAULT_WAL_SYNCHRONOUS=2',),
('ENABLE_FTS3',), ('ENABLE_RTREE',), ('SYSTEM_MALLOC',), ('THREADSAFE=1',)]
该列表中没有 ENABLE_FTS5
.
我尝试使用 Python 3.7.6(默认,2019 年 12 月 19 日,23:49:42)和 Python 3.6.7(默认,2018 年 10 月 25 日,09:16:13).
I tried with Python 3.7.6 (default, Dec 19 2019, 23:49:42) and Python 3.6.7 (default, Oct 25 2018, 09:16:13).
推荐答案
你会在连接上调用 .enable_load_extension(True)
和 .load_extension('fts5')
对象,而不是在模块上.
You would call .enable_load_extension(True)
and .load_extension('fts5')
on the connection object, and not on the module.
但是,这应该不是必需的,因为 - 正如您所看到的 - 您的安装支持全文搜索.
However, this shouldn't be necessary since -- as you've seen -- your installation supports full-text search.
您可以通过以下方式进行测试以确保:
Here's a way you can test that out just to be sure:
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute("""create virtual table fts5test using fts5 (data);""")
conn.execute("""insert into fts5test (data)
values ('this is a test of full-text search');""")
conn.execute("""select * from fts5test where data match 'full';""").fetchall()
结果:
In [67]: conn.execute("""select * from fts5test where data match 'full';""").fetchall()
Out[67]: [('this is a test of full-text search',)]
这篇关于如何在 Python 3.7 中使用带有 sqlite3 python 模块的 FTS5 扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!