Virtuoso SPASQL Support for pyodbc

Experimental support for using pyodbc to talk to the Virtuoso quad-store using SPASQL. It is for making the RDFLib Virtuoso Store work.

The implementation is tricky because pyodbc assumes that, as would be the case with most SQL servers, every value returned for a particular column is of the same type. This is not the case for literal values returned from a SPARQL query.

So basically what this does is:

  1. At connection time, check if we are connecting to a Virtuoso server
  2. At query execution time, check if the query starts with "SPARQL"
  3. For each column of each row, execute a bunch of Virtuoso-specific ODBC calls to find out the datatype and value.

We found further that Virtuoso was only reporting the real datatype if the query also contained define output:valmode "LONG" which returns the internal representation (IRI) of URI and blank node identifiers rather than their string representation. This means that a further SELECT is necessary to retrieve the string representation -- this is left to application code to do.

When processing a SPASQL query, the returned values are tuples of the form:

(value, dvtype, dttype, flags, lang, datatype)


It is up to the higher level code to take this tuple and transform it into whatever representation is appropriate (e.g. and rdflib.term.Node instance). This is different from straight SQL queries which should return the correct python datatype directly.


  1. Make sure you don't have pyodbc installed on your system

  2. Download pyodbc 2.1.8 and the patch against version 2.1.8

  3. Uncompress, patch and install:

    patch -p1 < pyodbc-spasql-preliminary.diff
    cd pyodbc-2.1.8
    python install

This newest patch will require version 0.11 or above of the RDFLib Virtuoso Store. If for some reason you are unable to upgrade, the old patch is still available.


Debian package diff, apply in addition to the above patch as well as packages:

To build on a different debian-like distribution, just download and uncompress the patched source then run:

make -k -f debian/rules binary

and you should have .deb packages one directory higher.