如何在Ubuntu 16.04上使用FTS5扩展和sqlite3-python模块以及python 3.7



为了测试带有sqlite3 Python模块的FTS5扩展是否有效,我使用了Vorspring durch Technik的以下代码:

import sqlite3     
conn = sqlite3.connect(':memory:')
conn.execute("""create virtual table fts5test using fts5 (data);""") 
conn.execute("""insert into fts5test (data) 
values ('this is a test of full-text search');""")
conn.execute("""select * from fts5test where data match 'full';""").fetchall() 

该代码在Ubuntu 18.04和Ubuntu 19.04上运行良好。例如,可以使用以下python3.7解释器通过Docker运行它:

docker pull ubuntu:18.04 # This line is optional.
docker run --interactive --tty ubuntu:18.04 bash
apt update
apt install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt update 
apt install -y python3.7
python3.7
# use here the python code given above 

然而,如果我将Ubuntu版本从18.04更改为16.04,那么FTS5扩展就不起作用了:

docker pull ubuntu:16.04 # This line is optional.
docker run --interactive --tty ubuntu:16.04 bash
apt update
apt install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt update 
apt install -y python3.7
python3.7
# use here the python code given above 

python代码将崩溃:

[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> conn.execute("""create virtual table fts5test using fts5 (data);""")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: no such module: fts5

如何在Ubuntu16.04上使用带有Python 3.7的sqlite3-Python模块的FTS5扩展?

这两篇文章(强制Python放弃本机sqlite3,使用(已安装的(最新sqlite3版本和http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/)可以解决这个问题。

尝试以下步骤:

  1. 下载SQLite源代码并在启用FTS5的情况下构建它。

    $ wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz
    $ tar xvzf sqlite-autoconf-3310100.tar.gz
    $ cd sqlite-autoconf-3310100
    $ CFLAGS="-DSQLITE_ENABLE_FTS5" ./configure
    $ make sqlite3.c
    
  2. 在继续之前,您可能需要激活您的虚拟环境。

    $ source <path to Python3.7 virtual environment>/bin/activate
    (python37env) $
    
  3. 这篇文章的作者(http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/)将Python 3中的SQLite3 DB-API 2.0驱动程序作为一个单独的包(在此处输入链接描述(。所以你可以用它来代替原来的驱动程序。

    (python37env) $ wget https://github.com/coleifer/pysqlite3/archive/0.4.2.tar.gz
    (python37env) $ tar xvzf 0.4.2.tar.gz
    (python37env) $ cd pysqlite3-0.4.2
    (python37env) $ cp <path to sqlite-autoconf-3310100>/sqlite3.h .
    (python37env) $ cp <path to sqlite-autoconf-3310100>/sqlite3.c .
    (python37env) $ python setup.py build_static build
    (python37env) $ python setup.py install
    

    现在,应该将单独的pysqlite模块安装到您的虚拟环境中。

  4. 测试它。不要使用import sqlite3来导入pysqlite3模块,而是使用from pysqlite3 import dbapi2 as sqlite3。这将从自定义pysqlite模块导入SQLite3 DB-API 2.0驱动程序。您的测试代码如下所示:

    # import sqlite3
    from pysqlite3 import dbapi2 as sqlite3
    conn = sqlite3.connect(':memory:')
    conn.execute("""create virtual table fts5test using fts5 (data);""")
    conn.execute("""insert into fts5test (data)
    values ('this is a test of full-text search');""")
    conn.execute("""select * from fts5test where data match 'full';""").fetchall()
    

这个方法适用于我的Ubuntu 16.04 VM和Python 3.7.3。

相关内容

最新更新