这是一个现成的示例
test.py:
import sqlite3db = sqlite3.connect(':memory:')db.enable_load_extension(True)db.load_extension('./spellfix') # for Linux#db.load_extension('./spellfix.dll') # <-- UNCOMMENT HERE FOR WINDOWSdb.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')]重要说明:距离editdist3已标准化,因此
值100用于插入和删除,值150用于替换
这是在Windows上首先要执行的操作:
下载https://sqlite.org/2016/sqlite-src-3110100.zip,https://sqlite.org/2016/sqlite-amalgamation-3110100.zip和解压他们
从此处替换
C:Python27DLLssqlite3.dll
为新的sqlite3.dll。如果跳过此步骤,您将在以后得到sqlite3.OperationalError: The specified procedure could not be found
跑:
call "C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat"
要么
call "C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat" x64cl /I sqlite-amalgamation-3110100/ sqlite-src-3110100/ext/misc/spellfix.c /link /DLL /OUT:spellfix.dllpython test.py
(使用MinGW,这将是:
gcc -g -shared spellfix.c -I ~/sqlite-amalgation-3230100/ -ospellfix.dll)
这是在Linux Debian上的方法:
apt-get -y install unzip build-essential libsqlite3-devwget https://sqlite.org/2016/sqlite-src-3110100.zipunzip sqlite-src-3110100.zipgcc -shared -fPIC -Wall -Isqlite-src-3110100 sqlite-src-3110100/ext/misc/spellfix.c -o spellfix.sopython test.py
以下是在具有旧Python版本的Linux Debian上执行此操作的方法:
如果您的发行版的Python有点旧,它将需要另一种方法。由于
sqlite3模块是内置在Python中的,因此对其进行升级似乎并不容易(
pipinstall --upgradepysqlite仅升级pysqlite模块,而不升级底层SQLite库)。因此,此方法例如在if
importsqlite3; print sqlite3.sqlite_version为3.8.2的情况下有效:
wget https://www.sqlite.org/src/tarball/27392118/SQLite-27392118.tar.gztar xvfz SQLite-27392118.tar.gzcd SQLite-27392118 ; sh configure ; make sqlite3.c ; cd ..gcc -g -fPIC -shared SQLite-27392118/ext/misc/spellfix.c -I SQLite-27392118/src/ -o spellfix.sopython test.py # [(1, u'hello world, guys')]



