It appears that pyspatialite is very outdated. Do I need a special library like pyspatialite in order to use the spatial functions of spatialite? Or can I just connect to the database via the normal python driver and execute the spatial functions? If so, is there a more updated library that I am not aware of?
1 Answer
You can connect to Spatialite via Python using the latest version of pysqlite instead of pyspatialite. Spatialite is just the spatial enablement of SQLite so this works, but if you are connecting in this way (via pysqlite) you need to load the libspatialite extension to be able to use the spatial functionality of Spatialite.
from pysqlite2 import dbapi as sqlite
conn = sqlite.connect("myDB")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("libspatialite-2.dll")')
curs = conn.cursor()
... do something interesting
Make sure you get the correct name for the libspatialite dll which may vary depending on your version, otherwise this won't work. Scroll down on the link above and you'll find the documentation for load_extension
.
-
1Very helpful! The current import is
from pysqlite2 import dbapi2 as sqlite3
. This latter for compatibility with Python 3 code.– gmas80Apr 19, 2015 at 4:17 -
1In case anyone falls here the import for current versions of python is
import sqlite3
instead offrom pysqlite2 import dbapi as sqlite
and of courceconn = sqlite3.connect("myDB")
Oct 21, 2019 at 11:57