强制 Python 放弃原生 sqlite3 并使用(已安装)最新的 sqlite3 版本 [英] Force Python to forego native sqlite3 and use the (installed) latest sqlite3 version

查看:61
本文介绍了强制 Python 放弃原生 sqlite3 并使用(已安装)最新的 sqlite3 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图摆脱的错误信息是:

<块引用>

属性错误:'sqlite3.Connection'对象没有属性'enable_load_extension'

我有'easy_install'-ed 最新的 sqlite3 版本,python 不知何故知道它在那里,因为 sqlite3.version_info 生成 3.6.13.在此版本中,连接应具有enable_load_extension"属性.

我认为正在发生的是 python 仍然使用我认为是 2.4.1 的原生 sqlite3 模块,因为 sqlite3.version (i.s.o. sqlite3.version_info) 产生 2.4.1.

问题是如何强制 python 对所有 sqlite3 调用使用新的 sqlite3 模块?

解决方案

sqlite3 在 Python 中的支持可能有点令人困惑.sqlite 数据库适配器开始是一个单独的项目,pysqlite2,但对于 Python 2.5,它的一个版本被合并了进入名为 sqlite3 的 Python 标准库.原始适配器继续作为单独的项目开发,同时定期更新 Python 本身的版本以匹配它.如果您尝试使用较新版本的适配器,通常将其安装为 pysqlite2,以免与标准库中包含的版本冲突.而且,根据它的构建方式,它可能会链接到不同版本的底层 sqlite3 数据库库.因此,请确保正确导入它:

<预><代码>>>>导入 sqlite3>>>sqlite3.version_info(2, 4, 1)>>>sqlite3.sqlite_version_info(3, 6, 11)>>>从 pysqlite2 导入 dbapi2 作为 sqlite3>>>sqlite3.version_info(2, 5, 5)>>>sqlite3.sqlite_version_info(3, 6, 18)

version_infosqlite3(pysqlite2 或内置 sqlite3)数据库适配器的版本.sqlite_version_info 是底层sqlite3 数据库库的版本.

建议使用 from ... import ... as sqlite3 以便在从一个版本移动到另一个版本时不需要更改其余代码.

注意,enable_load_extension 首次出现于 pysqlite2 2.5.0.

enable_load_extension 在您构建适配器时默认处于禁用状态.要启用它,您可以手动构建 pysqlite2.以下配方假设一个 unix-y 系统和最新版本的 pysqlite2,在撰写本文时为 2.5.5.

首先,如果您最初通过 easy_install 安装了适配器,请先运行以下命令将其卸载:

$ sudo/path/to/easy_install -m pysqlite # 或者你第一次使用的任何包名

会有一些输出,包括像这样的行:

从easy-install.pth文件中删除pysqlite 2.5.5使用/path/to/site-packages/pysqlite-2.5.5-py2.x-something.egg

使用列出的文件名删除 egg(名称会因您的平台和版本而异,它可能指一个文件或目录):

$ sudo rm -r/path/to/site-packages/pysqlite-2.5.5-py2.x-something.egg

现在下载并解压 pysqlite-2.5.5 源代码压缩包:

$ mkdir/tmp/build$ cd/tmp/build$ curl http://oss.itsystementwicklung.de/download/pysqlite/2.5/2.5.5/pysqlite-2.5.5.tar.gz |焦油$ cd pysqlite-2.5.5

然后编辑 setup.cfg 文件以注释掉 SQLITE_OMIT_LOAD_EXTENSION 指令:

$ ed setup.cfg <<EOF>/SQLITE_OMIT_LOAD_EXTENSION/s/define=/#define=/>瓦>q>EOF

由于 sqlite3 的版本太旧 (3.4.0),您还应该使用最新的 sqlite3 库进行构建.这在 pysqlite2 setup.py 脚本中很容易:

$/path/to/python2.x setup.py build_static

这将自动下载最新的sqlite3 合并源并构建适配器以及sqlite3 的最新静态链接版本.此步骤可能需要很长时间才能完成.

UPDATE (2015/07/21):根据最新pysqlite 2.6.3 commit必须自己下载sqlite源代码并将它们放在pysqlite根文件夹中.

现在,安装适配器:

$ sudo/path/to/python2.x setup.py install

并运行测试:

$ cd # 构建目录之外的某处$/path/to/python2.x>>>从 pysqlite2 导入测试>>>测试一下()

而且,如果他们通过了,你应该准备好了.

作为奖励,如果您希望加载扩展支持的原因是使用 sqlite3 的全文搜索扩展,FTS3,您应该发现它被包含为静态库的一部分,不需要进一步的工作:

<预><代码>>>>从 pysqlite2 导入 dbapi2 作为 sqlite3>>>con = sqlite3.connect(":memory:")>>>con.execute("使用fts3(name,成分)创建虚拟表配方")<pysqlite2.dbapi2.Cursor 对象在 0xca5e0>

The error message I am trying to get rid of is:

AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

I have 'easy_install'-ed the latest sqlite3 version and python somehow know it is there since sqlite3.version_info produces 3.6.13. In this version the Connection should have the 'enable_load_extension' attribute.

What I think is going on is that python still uses the native sqlite3 module which I think is 2.4.1 since sqlite3.version (i.s.o. sqlite3.version_info) produces 2.4.1.

The question is how do I force python to use the new sqlite3 module for all sqlite3 calls?

解决方案

sqlite3 support in Python can be a bit confusing. The sqlite database adapter started out as a separate project, pysqlite2, but for Python 2.5 a version of it was incorporated into the Python standard library under the name sqlite3. The original adapter continues to be developed as that separate project while periodically the version in Python itself is updated to match it. If you are trying to use a newer version of the adapter, it is usually installed as pysqlite2 so as not to conflict with the version included in the standard library. And, depending how it was built, it may link to a different version of the underlying sqlite3 database library. So make sure you are importing it properly:

>>> import sqlite3
>>> sqlite3.version_info
(2, 4, 1)
>>> sqlite3.sqlite_version_info
(3, 6, 11)

>>> from pysqlite2 import dbapi2 as sqlite3
>>> sqlite3.version_info
(2, 5, 5)
>>> sqlite3.sqlite_version_info
(3, 6, 18)

version_info is the version of the sqlite3 (pysqlite2 or built-in sqlite3) database adapter. sqlite_version_info is the version of the underlying sqlite3 database library.

Using from ... import ... as sqlite3 is suggested so that the rest of your code does not need to change if you move from one version to the other.

Note, enable_load_extension first appeared in pysqlite2 2.5.0.

EDIT: enable_load_extension is disabled by default when you build the adapter. To enable it, you can build pysqlite2 manually. The following recipe assumes a unix-y system and the lastest version of pysqlite2, which as of this writing is 2.5.5.

First, if you installed the adapter originally via easy_install, uninstall it by first running:

$ sudo /path/to/easy_install -m pysqlite # or whatever package name you first used

There will be some output from that including lines like:

Removing pysqlite 2.5.5 from easy-install.pth file

Using /path/to/site-packages/pysqlite-2.5.5-py2.x-something.egg

Remove the egg using the file name listed (the name will vary depending on your platform and version and it may refer to a file or a directory):

$ sudo rm -r /path/to/site-packages/pysqlite-2.5.5-py2.x-something.egg

Now download and extract the pysqlite-2.5.5 source tarball:

$ mkdir /tmp/build
$ cd /tmp/build
$ curl http://oss.itsystementwicklung.de/download/pysqlite/2.5/2.5.5/pysqlite-2.5.5.tar.gz | tar xz
$ cd pysqlite-2.5.5

Then edit the setup.cfg file to comment out the SQLITE_OMIT_LOAD_EXTENSION directive:

$ ed setup.cfg <<EOF
> /SQLITE_OMIT_LOAD_EXTENSION/s/define=/#define=/
> w
> q
> EOF

Since the version of sqlite3 is so old (3.4.0), you should also build with the latest sqlite3 library. This is made easy in the pysqlite2 setup.py script:

$ /path/to/python2.x setup.py build_static

This will automatically download the latest sqlite3 amalgamation source and build the adapter along with an up-to-date statically-linked version of sqlite3. This step may take a long while to finish.

UPDATE (2015/07/21): According to the latest pysqlite 2.6.3 commit you have to download sqlite source code by yourself and put them in pysqlite root folder.

Now, install the adapter:

$ sudo /path/to/python2.x setup.py install

and run the tests:

$ cd     # somewhere out of the build directory
$ /path/to/python2.x
>>> from pysqlite2 import test
>>> test.test()

and, if they pass, you should be all set.

As a bonus, if the reason you want load extension support is to use sqlite3's full text search extension, FTS3, you should find that it was included as part of the static library and no further work is necessary:

>>> from pysqlite2 import dbapi2 as sqlite3
>>> con = sqlite3.connect(":memory:")
>>> con.execute("create virtual table recipe using fts3(name, ingredients)")
<pysqlite2.dbapi2.Cursor object at 0xca5e0>

这篇关于强制 Python 放弃原生 sqlite3 并使用(已安装)最新的 sqlite3 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆