I want to edit a specific field for a QgsVectorLayer, within a QGIS application.
I am facing inconsistent results from my code, ie: changes are not alway commited. Please could someone point me to where it is wrong within this loop:
#vlayer is a QgsVectorLayer, imported from SQLITE DB
vlayer = QgsMapLayerRegistry.instance().mapLayer(layer)
caps = vlayer.dataProvider().capabilities()
polygonGeom = QgsGeometry( rubberBand.asGeometry() )
#if vlayer is capable of edition
if caps & QgsVectorDataProvider.ChangeAttributeValues:
vlayer.selectAll()
for feature in vlayer.selectedFeatures():
if feature.geometry() == None:
continue
if feature.geometry().intersects(polygonGeom):
fid = feature.id()
idx = vlayer.fieldNameIndex('some field')
attr ={idx:'some value'}
print fid,idx,attr
vlayer.dataProvider().changeAttributeValues({fid:attr})
vlayer.invertSelection()
Here is a post of my console showing that some features are selected and should be edited, but again, some are not properly updated to the underlying database.
2 16 {16: 4}
12 16 {16: 4}
13 16 {16: 4}
vlayer.selectAll(). Justfor feature in vlayer.getFeatures()– Nathan W Aug 20 '14 at 10:15vlayer.updateFields()without success also) – Peter Peterson Aug 20 '14 at 10:28QgsVectorFileWriter.writeAsVectorFormat(layer, layer.name(),"utf-8", None, "SQLite", False, None ,["SPATIALITE=YES",])and the way I reloaded them to my applicationvlayer = QgsVectorLayer(vlayerpath, tablename, "spatialite"). And the output ofvlayer = QgsMapLayerRegistry.instance().mapLayer('some name')gives me 'ogr' – Peter Peterson Aug 20 '14 at 10:46