如何在SQLite中使用editdist3 [英] How to use editdist3 in sqlite
问题描述
根据回答另一个问题,在sqlite中,Levenshtein距离是通过称为editdist3
的SQL函数实现的. (还要比较文档)
According to an answer to another question, in sqlite the Levenshtein distance is implemented in a SQL function called editdist3
. (Compare also the documentation)
现在,当我尝试使用它时,我得到的只是一个不存在的错误:
Now when I try to use it, all I get is an error that it doesn’t exist:
╰┄┄> 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标志icu
,readline
和secure-delete
.
I’m using sqlite-3.11.1 on Gentoo Linux with (default) USE flags icu
, readline
and secure-delete
.
推荐答案
事实证明editdist3
包含在必须显式加载的sqlite扩展中.由于我在Gentoo sqlite软件包中找不到它,因此我也必须自己构建它.正如文档所述:
It turns out editdist3
is contained in an sqlite extension that has to be loaded explicitly. As I didn’t find it in the Gentoo sqlite package, I also had to build it myself. As the documentation says:
spellfix1虚拟表未包含在SQLite合并中 而不是任何标准SQLite构建的一部分.这是一个可加载的 扩展名.
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
.
Note that sqlite automatically appends the extension .so
.
要在python中使用它(这是我的初衷),需要完成以下操作:
To use it in python – which was my original intention – the following needs to be done:
db = sqlite3.connect(':memory:')
db.enable_load_extension(True)
db.load_extension('./spellfix')
db.enable_load_extension(False)
这篇关于如何在SQLite中使用editdist3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!