Monday, 2 February 2015

qgis - PyQGIS - load PostGIS layer as editable


I can load a PostGIS layer on QGIS' map canvas but "Toggle Editing" button comes "inactive". How can I load vector layer as editable?


The code given below loads PostGIS layer on map canvas & "Toggle Editing" button comes "active" but once I run the script below in Python console; when I press "Toggle Editing" button, QGIS gives me an info message: "Start editing failed: Provider cannot be opened for editing":


sql = "(SELECT * FROM tablename)"
uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "database", "postgres", "password")
uri.setDataSource("", sql, "geom", "", "gid")
vlayer = QgsVectorLayer(uri.uri(), "abc", "postgres")

QgsMapLayerRegistry.instance().addMapLayer(vlayer)

# should the layer be set as "active" ???

vlayer.dataProvider()
vlayer.dataProvider().capabilities()
vlayer.startEditing()
iface.actionToggleEditing().setEnabled(True)

Can someone please give me a hint on how to load an editable layer programmatically?





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...