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 aspysqlite2
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:version_info
is the version of thesqlite3
(pysqlite2
or built-insqlite3
) database adapter.sqlite_version_info
is the version of the underlyingsqlite3
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 inpysqlite2
2.5.0.EDIT:
enable_load_extension
is disabled by default when you build the adapter. To enable it, you can buildpysqlite2
manually. The following recipe assumes aunix
-y system and the lastest version ofpysqlite2
, which as of this writing is 2.5.5.First, if you installed the adapter originally via
easy_install
, uninstall it by first running:There will be some output from that including lines like:
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):
Now download and extract the
pysqlite-2.5.5
source tarball:Then edit the
setup.cfg
file to comment out theSQLITE_OMIT_LOAD_EXTENSION
directive:Since the version of
sqlite3
is so old (3.4.0), you should also build with the latestsqlite3
library. This is made easy in thepysqlite2
setup.py script: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:
and run the tests:
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:Instead of the standard Python
sqlite3
module, you might be able to use the apsw module, a third-party SQLite module that more closely follows the SQLite API. It includes support for loading extensions, as well as basically anything that is allowed in the SQLite C/C++ API. It also tries as much as possible to keep up to date with any new changes in SQLite.You need to look at your Python path and make sure that the sqlite you want is installed in an earlier directory than the built-in sqlite.
You can see the path with:
If you want to find out where a module is from, try:
I have python 2.7 on a windows machine and the built-in sqlite3.sqlite_version was 3.6.x. By performing the following steps I was able to get it to use sqlite 3.7.9.