2

Is it possible to load pure sqlite 2.0 (without spatial extension) table into qgis using python? I would like to get the same result as in the case of using Add Vector Layer -> All files. I have already modified this example http://www.qgis.org/pyqgis-cookbook/loadlayer.html but it doesn't work in my case. Can someone help me? I use in the python console this code:

>>> db = r'c:\Program Files (x86)\Quantum GIS Lisboa\data\base.db'
>>> uri = QgsDataSourceURI()
>>> uri.setDatabase(db)
>>> schema = ''
>>> table = 'table'
>>> geom_column = ''
>>> uri.setDataSource(schema, table, geom_column)
>>> display_name = 'table'
>>> vlayer = QgsVectorLayer(uri.uri(), display_name, 'sqlite')
>>> QgsMapLayerRegistry.instance().addMapLayer(vlayer)
<qgis.core.QgsVectorLayer object at 0x0CFA08A0>

i also tried with 'spatialite' in vlayer. When I use Add vector layer i can see result table in the TOC. Using this code I can't display table in table of contents. I think the reason of my trouble is trivial, but I have no idea what I should do to see my table

2
  • What means "doesn't work"? Is there any error message or other feedback? Can you share your current code. Commented Aug 2, 2013 at 16:33
  • Using this part of code: pathLayer = 'c: +\database.db|layername=layer1' vlayer = QgsVectorLayer(pathLayer, 'layer1', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(vlayer) i can add one table. Using Uri I have never get a result. Hot to set parameters of connection for geometryless table?
    – ConeGIS
    Commented Aug 4, 2013 at 23:00

2 Answers 2

2

If you are able to add a layer manually (Add Layer Dialog), you can get from it all the information needed to add it again via python.

Select the layer in the TOC

l = qgis.utils.iface.activeLayer()
p = l.dataProvider()

key = l.providerType()
uri = p.dataSourceUri()

QgsMapLayerRegistry.instance().addMapLayer( QgsVectorLayer( uri, 'layer', key ) )

In your case the key is 'spatialite'

2

ConeGIS, you have:

geom_column = ''
uri.setDataSource(schema, table, geom_column)

you want:

geom_column = None
uri.setDataSource(schema, table, geom_column)

(working for me in QGis 2.0.1)

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.