3

In QGIS I've got a Spatialite database containing tables without a geometry column. With the QGIS GUI it is perfectly possible to add such a table as a 'Spatialite vector layer'. But how to do this in Python?

On the console I've tried:

>>> uri = QgsDataSourceURI()
>>> uri.setDatabase('/<path>/test.sqlite')
>>> uri.setDataSource('', 'TestTableName','','','id')
>>> vlayer = QgsVectorLayer(uri.uri(), 'TestLayer', 'spatialite')
>>> QgsMapLayerRegistry.instance().addMapLayer(vlayer)

No result

>>> vlayer.isValid()
False

What is going wrong?

Some extra analysis: When I add this layer by the GUI the uri-description is as follows:

>>> qgis.utils.iface.activeLayer().dataProvider().dataSourceUri()
PyQt4.QtCore.QString(u'dbname=\'/<path>/test.sqlite\' table="TestTableName" sql=')

When I add this layer by the Python console as described above, the uri-description is somewhat different:

>>> uri.uri()
PyQt4.QtCore.QString(u'dbname=\'/<path>/test.sqlite\' table="TestTableName" () sql=')

Could the empty parentheses cause the different behaviour? So maybe QGIS 1.8.0 handles empty parentheses different as no parentheses?

4
  • The exact code you posted works perfectly here (with QGIS 1.9) Commented Sep 9, 2013 at 16:04
  • @gene. Hmm, weird. I'm using QGIS 1.8.0 on OS X. It only works for me when I use a table with a geometry column, so something must go wrong in line: >>> uri.setDataSource('',<table_name>,'') As adding this layer with the GUI works fine, maybe QGIS is using different code for non geometry tables? Commented Sep 10, 2013 at 6:47
  • The parentheses in the uri cause the problem. I've solved it as follows: uri2 = QgsDataSourceURI(uri.uri().remove(' ()')) vlayer = QgsVectorLayer(uri2.uri(), 'TestLayer', 'spatialite') vlayer.isValid() True It looks like this problem does not occur in QGIS 1.9. Same issue ( and same solution -> thanks a lot Spingerfitz) with postgres and QGIS 2.12 Commented May 19, 2016 at 13:44
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Kersten
    Commented May 19, 2016 at 14:23

2 Answers 2

4

Finally, I got it working. My analysis seems right. The parentheses in the uri cause the problem. I've solved it as follows:

>>> uri2 = QgsDataSourceURI(uri.uri().remove(' ()'))
>>> vlayer = QgsVectorLayer(uri2.uri(), 'TestLayer', 'spatialite')
>>> vlayer.isValid()
True

It looks like this problem does not occur in QGIS 1.9 >.

2

Nothing

With a SQLite layer (not spatial)

uri = QgsDataSourceURI()
uri.setDatabase('Midv_31370.sqlite')
uri.setDataSource('', 'obs_points','','','obsid')
vlayer = QgsVectorLayer(uri.uri(), 'TestLayer2', 'spatialite')
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
vlayer.isValid()
True

enter image description here

If you want a spatial layer (Spatialite), you must indicate the geometry column:

uri = QgsDataSourceURI()
uri.setDatabase('Midv_31370.sqlite')
uri.setDataSource('', 'obs_points', 'geometry')
vlayer = QgsVectorLayer(uri.uri(), 'TestLayer2', 'spatialite')
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
vlayer.isValid()
True

enter image description here

1
  • Hmm, weird. I'm using QGIS 1.8.0 on OS X. It only works for me when I use a table with a geometry column, so something must go wrong in line: >>> uri.setDataSource('',<table_name>,'') Commented Sep 10, 2013 at 6:35

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.