简体   繁体   English

python和sqlite3,请检查是否可以使用fts5扩展名?

[英]python and sqlite3, check if I can use fts5 extension?

I recently found out that FTS5 extension has been released. 我最近发现FTS5扩展已发布。
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 ?? 只需根据发布页面进行 python3版本检查或sqlite3.sqlite_version检查? Or something else? 或者是其他东西?

/ this was previously edit of the OP post, but I moved it down here to keep the question clear /这是先前OP帖子的编辑,但我将其移至此处以使问题更清楚

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. 但是奇怪的是,我在几个虚拟机上运行它,并且都没有启用fts4,但是我却像没有任何东西一样愉快地使用它……也许它的fts3 / fts4都一样,或者可能都是错误的。

/edit /编辑
from the documentation 从文档中

Note that enabling FTS3 also makes FTS4 available. 请注意,启用FTS3也会使FTS4可用。 There is not a separate SQLITE_ENABLE_FTS4 compile-time option. 没有单独的SQLITE_ENABLE_FTS4编译时选项。 A build of SQLite either supports both FTS3 and FTS4 or it supports neither. SQLite的版本要么支持FTS3和FTS4,要么不支持。

The documentation you link to mentions that FTS5 is disabled by default. 链接到的文档提到默认情况下FTS5是禁用的。 Did you enable it when compiling SQLite? 在编译SQLite时是否启用了它?

One quick way to know is to use the peewee ORM : 一种快速了解的方法是使用peewee ORM

from playhouse.sqlite_ext import FTS5Model
FTS5Model.fts5_installed()

The above will return True if you can use FTS5. 如果可以使用FTS5,则以上内容将返回True You can install peewee with pip install peewee . 您可以安装peeweepip install peewee

You could also use the apsw wrapper, which includes FTS5 by default since version 3.11.0-r1 . 您也可以使用apsw包装器, 自版本3.11.0-r1起默认包含FTS5 See the build instructions and use the --enable-all-extensions flag. 请参阅构建说明并使用--enable-all-extensions标志。 The apsw wrapper uses the amalgamation . apsw包装器使用合并

EDIT: Here is the code from the peewee source demonstrating how this is done: 编辑:这是来自peewee的代码,展示了如何完成此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM