[Qgis-developer] Use of Spatialite in a Python plugin for QGIS?

Stefan Keller sfkeller at gmail.com
Thu Apr 28 20:00:11 EDT 2011


We are currently developing a Python Plugin for QGIS. We use
SpatiaLite to manage some data. What we want to do is, querying a
SpatiaLite database from a Python Plugin. We were able to get it to
work with SQLite, using the following code:

self.db = QSqlDatabase.addDatabase("QSQLITE")
if self.db.open() == False:
    raise "Connection Error!"
query = QSqlQuery(self.db)
query.exec_("SELECT sqlite_version()")
if query.isActive():
    print str(query.value(0).toString()) #
    raise str(query.lastError().databaseText ())

But when we try using a spatialite-function, like "SELECT
spatialite_version()", the query fails with "no such function:
spatialite_version". We tried to load the spatialite-Extension
manually, using "SELECT load_extension('libspatialite-2.dll');", but
that resulted in an exception "not authorized".

Our question is: What has to be done to use spatialite in a python-plugin?

There is pyspatialite(http://code.google.com/p/pyspatialite/), but we
would prefer using libraries already included in QGIS.
Thanks for your help.

Yours, Stefan and Tobias

More information about the Qgis-developer mailing list