5

I recently found out that FTS5 extension has been released.
What is the best way to check if my application can use it on users system?
Just python3 version check, or sqlite3.sqlite_version check according to release page?? Or something else?

DoTheEvo
  • 874
  • 1
  • 9
  • 21
  • The answer does not belong into the question but into an answer (you can answer your own question). If you have a new question, ask a new question. – CL. Apr 16 '16 at 07:36

2 Answers2

12

/ this was previously edit of the OP post, but I moved it down here to keep the question clear

so this feels like it could work, found it in the question here

import sqlite3

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

print(available_pragmas)

if ('ENABLE_FTS5',) in available_pragmas:
    print('YES')
else:
    print('NO')

But what is strange is that I run it in a few virtual machines, and none had fts4 enabled, yet I used it happily like nothing... maybe its fts3/fts4 being rather the same, or maybe its just all wrong.

/edit
from the documentation

Note that enabling FTS3 also makes FTS4 available. There is not a separate SQLITE_ENABLE_FTS4 compile-time option. A build of SQLite either supports both FTS3 and FTS4 or it supports neither.

Community
  • 1
  • 1
DoTheEvo
  • 874
  • 1
  • 9
  • 21
4

The documentation you link to mentions that FTS5 is disabled by default. Did you enable it when compiling SQLite?

One quick way to know is to use the peewee ORM:

from playhouse.sqlite_ext import FTS5Model
FTS5Model.fts5_installed()

The above will return True if you can use FTS5. You can install peewee with pip install peewee.

You could also use the apsw wrapper, which includes FTS5 by default since version 3.11.0-r1. See the build instructions and use the --enable-all-extensions flag. The apsw wrapper uses the amalgamation.

EDIT: Here is the code from the peewee source demonstrating how this is done:

def fts5_installed(cls):
    if sqlite3.sqlite_version_info[:3] < FTS5_MIN_VERSION:
        return False

    # Test in-memory DB to determine if the FTS5 extension is installed.
    tmp_db = sqlite3.connect(':memory:')
    try:
        tmp_db.execute('CREATE VIRTUAL TABLE fts5test USING fts5 (data);')
    except:
        try:
            sqlite3.enable_load_extension(True)
            sqlite3.load_extension('fts5')
        except:
            return False
        else:
            cls._meta.database.load_extension('fts5')
    finally:
        tmp_db.close()

    return True
ChrisP
  • 5,812
  • 1
  • 33
  • 36
  • Bringing frameworks I dont use in to this for just checking seems rather not ideal. – DoTheEvo Apr 15 '16 at 21:10
  • 4
    @DoTheEvo, Yeah, but you could read the [peewee source](https://github.com/coleifer/peewee/blob/master/playhouse/sqlite_ext.py) for yourself and see how it's done! The function is just 19 lines; you don't need to actually use `peewee`, just copy the code you need. – ChrisP Apr 16 '16 at 00:43
  • 1
    [Disclosure: I am the apsw author] There is no 100% certain way to find out if the extension is present other than by trying to use it. The extension could be compiled as part of SQLite or separately loadable. There is no guarantee either that 'fts5' is the one you are expecting. In any event the code above is the most robust way to check. – Roger Binns Apr 16 '16 at 18:41