python-3.xsqliteubuntufts5

How can I use the FTS5 extension with the sqlite3 python module with Python 3.7?


How can I use the FTS5 extension with the sqlite3 python module with Python 3.7?

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

which results in the error message:

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'

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.

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

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

which seems to indicate that the FTS5 is available in my /usr/local/bin/sqlite3 version.

However when I run

import sqlite3

con = sqlite3.connect(':memory:')
cur = con.cursor()
cur.execute('pragma compile_options;')
available_pragmas = cur.fetchall()
con.close()

print(available_pragmas)

it outputs:

[('COMPILER=gcc-5.4.0 20160609',), ('DEFAULT_SYNCHRONOUS=2',), ('DEFAULT_WAL_SYNCHRONOUS=2',), 
('ENABLE_FTS3',), ('ENABLE_RTREE',), ('SYSTEM_MALLOC',), ('THREADSAFE=1',)]

There is no ENABLE_FTS5 in that list.

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


Solution

  • 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() 
    

    Result:

    In [67]: conn.execute("""select * from fts5test where data match 'full';""").fetchall()
    Out[67]: [('this is a test of full-text search',)]