What are QGIS equivalent functions for ArcPy's Update/Delete Row/Field?

The following examples are based on the (soon-to-be-released) QGIS 2.0 API. This has changed since 1.8, so the syntax for this version will differ in some places.

The best resource for 1.8 compliant solutions and in-depth explanation for background of parameters (also apply for 2.0) are the PyQGIS Cookbook

We'll assume you already have a reference to a vector layer called vl obtained by e.g.

vl = iface.activeLayer()

Transactions

The following examples are working in a transaction (locally cached in QGIS). Before starting the edit session you have to call

vl.startEditing()

and end it with

vl.commitChanges()

to write your changes to the data source. or

vl.rollBack()

to dismiss the changes

You could also work directly on vl.dataProvider() and forget about the transaction control statements. (Resulting in autocommit-like behaviour)

Adding a field

QgsVectorLayer.addAttribute( QgsField )

from PyQt4.QtCore import QVariant
vl.addAttribute( QgsField( 'fieldname', QVariant.String ) )

This only works, if the dataprovider implements the AddAttributes capability. You can check this with:

if vl.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues

Calculate fields

See this answer:

Is it possible to programmatically add calculated fields?

It's still aimed at 1.8, so instead of vl.select() you have to call the 2.0 equivalent vl.getFeatures() ( see QGIS API changes )

Update rows

QgsVectorLayer.updateField( featureId, fieldIndex, value )

vl.updateField( 1000, 5, 'hello' )

Preqrequisites check (optional):

if vl.dataProvider().capabilities() & QgsVectorDataProvider.ChangeAttributeValues

To know the fieldIndex, you can use QgsVectorLayer.pendingFields()

Edit: See also the comment by NathanW which mentions the well readable QgsVectorLayer.updateFeature( feature ) method.

Delete rows

QgsVectorLayer.deleteFeature( featureId )

vl.deleteFeature( 1000 )

Optional prerequisites check:

if vl.dataProvider().capabilities() & QgsVectorDataProvider.DeleteFeatures
    

This is how you would do your code in PyQGIS:

keep = ["Bob","Janet","John","Mike"]

for counter, feature in enumerate(layer.getFeatures()):
    if feature['Name'] in keep:
        feature['ID'] = counter
        layer.updateFeature(feature)
    else:
        layer.deleteFeature(feature.id())

Tags:

Qgis

Pyqgis