I want to develop a simple plugin and try to get started with Python in QGIS.
However, I have already run into troubles just loading my data properly. From the cook book I am using the code below successfully to add my layer in QGIS. But it won't retrieve information from my layer:
uri = QgsDataSourceURI() #set host name, port , database name, username and password uri.setConnection("localhost", "5432", "local_db", "postgres", "postgres") #set database schema, table name, geometry column and optionaly subset(WHERE clause) uri.setDataSource ("friluftsliv", "climbing", "geom") # Defining the layer name and layer type for QGIS? climbing=QgsVectorLayer (uri .uri() ,"Climbing","postgres") # Adding a layer to registry QgsMapLayerRegistry.instance().addMapLayer(climbing)
Now I am trying to retrieve information from my layers using:
for field in climbing.pendingFields(): print field.id_0()
This does not give me anything, no errors nor info... I have 27 rows in my table. It has SERIAL and PK in database. From http://www.qgistutorials.com/en/docs/getting_started_with_pyqgis.html I tried with nothing to be printed.
for field in layer.getFeatures(): print field['id_0']
I have another layer called hiking that I try to add with the same code as above. That layer don't get added to the registry. I don't get any errors when I defining the layer but if I run QgsMapLayerRegistry.instance().mapLayers() it does not come up in the list...
Any advice on what might be wrong?