I am trying to load the Spaltialite extension using SQLite3 python module. I am getting the following error when I run my script.
EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host
import sqlite3
conn = sqlite3.connect(r'C:\folder1\TestDB.sqlite')
conn.enable_load_extension(True)
conn.execute("select load_extension('libspatialite-4.dll')")
for row in conn.execute('SELECT AsText(Geometry) FROM alaska'):
print row
I am able to connect to this db and run a non-spatial query without any issues.
Best Answer
In native (out of the box) Python installations extensions are disabled within sqlite3. Unfortunately this means that you will need to build the base pysqlite2 library manually and use that instead.
On Windows this will mean that you need a C compiler (Visual Studio comes with one - I've used Visual Studio 2010 for this before). However (with massive props) Christoph Gholke's Unofficial Windows Binaries for Python Extension Packages provides a pysqlite2 download that does have the extensions enabled by default. I haven't tested these binaries with 3rd party extensions though, so for a guarantee I'd try building it myself.
setup.cfg
comment out thedefine=SQLITE_OMIT_LOAD_EXTENSION
so the config looks like:[build_ext] #define= #include_dirs=/usr/local/include #library_dirs=/usr/local/lib libraries=sqlite3 #define=SQLITE_OMIT_LOAD_EXTENSION
python setup.py build_static install
. This will download and build the sqlite3 libraries for you.We can then test this in Python with the following:
Assuming that's all worked with no errors thrown then you should be good to go using spatialite in Python.