Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
    
What means "doesn't work"? Is there any error message or other feedback? Can you share your current code. –  Matthias Kuhn Aug 2 '13 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 Aug 4 '13 at 23:00

2 Answers 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'

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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