Tuesday, June 26, 2012

Introduction to the Update Cursor (arcpy.da) at 10.1

The UpdateCursor creates a read-write access to records returned from a feature class or table.
Returns an iterator of lists. The order of values in the list matches the order of fields specified by the field_names argument.  UpdateCursor objects can be iterated using a for loop and it supports with statements. Using a with statement will ensure that the database locks are removed.

You can use both an insert and update cursor at the same time if an edit session is opened.


from arcpy import da
fc = r"c:\temp\samples.gdb\nests"
fields = ('BirdPop', 'Rank')

with da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if (row[0] >= 0 and row[0] <= 10):
            row[1] = 1
        elif (row[0] > 10 and row[0] <= 20):
            row[1] = 2
        elif (row[0] > 20 and row[0]<= 30):
            row[1] = 3
        elif (row[0] > 20):
            row[1] = 4

In this simple example, you see that you can use conditional statements to change values within the 'with' statement.



Randy Long said...

The DB locks, are these schema locks?

Andrew said...