如何在sqlite中使用editdist3

How to use editdist3 in sqlite

根据 answer to another question, in sqlite the Levenshtein distance is implemented in a SQL function called editdist3. (Compare also the documentation)

现在当我尝试使用它时,我得到的只是一个它不存在的错误:

╰┄┄> sqlite3
SQLite version 3.11.1 2016-03-03 16:17:53
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE test (col1 TEXT);
sqlite> INSERT INTO test VALUES ('foobar');
sqlite> SELECT * FROM test WHERE editdist3(col1, 'f00bar') < 3;
Error: no such function: editdist3

我在 Gentoo Linux 上使用 sqlite-3.11.1,带有(默认)USE 标志 icureadlinesecure-delete

事实证明 editdist3 包含在必须显式加载的 sqlite 扩展中。由于我没有在 Gentoo sqlite 包中找到它,所以我也不得不自己构建它。正如 documentation 所说:

The spellfix1 virtual table is not included in the SQLite amalgamation and is not a part of any standard SQLite build. It is a loadable extension.

首先我获取了源代码

wget https://sqlite.org/2016/sqlite-src-3110100.zip
unzip sqlite-src-3110100.zip

那就得编译了

gcc -shared -fPIC -Wall -Isqlite-src-3110100 sqlite-src-3110100/ext/misc/spellfix.c -o spellfix.so

终于可以加载了

.load ./spellfix

请注意,sqlite 会自动附加扩展名 .so

要在 python 中使用它——这是我的初衷——需要完成以下工作:

db = sqlite3.connect(':memory:')

db.enable_load_extension(True)
db.load_extension('./spellfix')
db.enable_load_extension(False)