这是一个随时可用的示例test.py
:
import sqlite3
db = sqlite3.connect(':memory:')
db.enable_load_extension(True)
db.load_extension('./spellfix') # for Linux
#db.load_extension('./spellfix.dll') # <-- UNCOMMENT HERE FOR WINDOWS
db.enable_load_extension(False)
c = db.cursor()
c.execute('CREATE TABLE mytable (id integer, description text)')
c.execute('INSERT INTO mytable VALUES (1, "hello world, guys")')
c.execute('INSERT INTO mytable VALUES (2, "hello there everybody")')
c.execute('SELECT * FROM mytable WHERE editdist3(description, "hel o wrold guy") < 600')
print c.fetchall()
# Output: [(1, u'hello world, guys')]
重要提示:距离编辑距离3 https://sqlite.org/spellfix1.html被标准化,以便
值100用于插入和删除,150用于替换
以下是在 Windows 上首先要做的事情:
下载https://sqlite.org/2016/sqlite-src-3110100.zip https://sqlite.org/2016/sqlite-src-3110100.zip, https://sqlite.org/2016/sqlite-amalgamation-3110100.zip https://sqlite.org/2016/sqlite-amalgamation-3110100.zip并解压它们
Replace C:\Python27\DLLs\sqlite3.dll
由新的sqlite3.dll https://www.sqlite.org/download.html from here https://www.sqlite.org/download.html。如果跳过这个,你会得到一个sqlite3.OperationalError: The specified procedure could not be found
later
-
Run:
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
or
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64
cl /I sqlite-amalgamation-3110100/ sqlite-src-3110100/ext/misc/spellfix.c /link /DLL /OUT:spellfix.dll
python test.py
(对于 MinGW,它将是:gcc -g -shared spellfix.c -I ~/sqlite-amalgation-3230100/ -o spellfix.dll
)
以下是在 Linux Debian 上执行此操作的方法:
(基于这个答案 https://stackoverflow.com/a/36427821/1422096)
apt-get -y install unzip build-essential libsqlite3-dev
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
python test.py
以下是在使用较旧 Python 版本的 Linux Debian 上执行此操作的方法:
如果您的发行版的 Python 有点旧,则需要另一种方法。作为sqlite3
模块是Python内置的,看起来不简单 https://github.com/ghaering/pysqlite/issues/123#issuecomment-381447917升级它(pip install --upgrade pysqlite
只会升级 pysqlite 模块,而不是底层 SQLite 库)。因此这个方法 https://stackoverflow.com/a/49847451/1422096例如,如果import sqlite3; print sqlite3.sqlite_version
是3.8.2:
wget https://www.sqlite.org/src/tarball/27392118/SQLite-27392118.tar.gz
tar xvfz SQLite-27392118.tar.gz
cd SQLite-27392118 ; sh configure ; make sqlite3.c ; cd ..
gcc -g -fPIC -shared SQLite-27392118/ext/misc/spellfix.c -I SQLite-27392118/src/ -o spellfix.so
python test.py # [(1, u'hello world, guys')]