I want to load a PostGIS layer to QGIS canvas. I am using the following code but it is not loading the layer in canvas.
sql = "(SELECT * FROM table WHERE id = 20)"
uri = QgsDataSourceURI()
uri.setConnection("host", "5432", "databasename", "user", "password")
uri.setDataSource("public","table", "the_geom", sql,"gid")
print uri.uri()
vlayer = QgsVectorLayer(uri.uri(), "test", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
canvas.refresh()
But the layer is not loaded. I don't know what I am doing wrong here.
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Oct 5, 2015 at 7:59
-
what does vlayer.isValid() outputing ?SIGIS– SIGIS2016年08月22日 06:35:52 +00:00Commented Aug 22, 2016 at 6:35
1 Answer 1
The sql-parameter is not what it sounds like. It only takes a where-clause, like 'id = 20' in your case. So try:
uri.setDataSource("public","table", "the_geom", 'id = 20',"gid")
answered Oct 4, 2016 at 8:30
default