7

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 1

13

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.

2
  • 1
    Very helpful! The current import is from pysqlite2 import dbapi2 as sqlite3. This latter for compatibility with Python 3 code.
    – gmas80
    Apr 19, 2015 at 4:17
  • 1
    In case anyone falls here the import for current versions of python is import sqlite3 instead of from pysqlite2 import dbapi as sqlite and of cource conn = sqlite3.connect("myDB") Oct 21, 2019 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.