I am trying to write a script that iterates through each row of a table, calculates for this row something. The result of this calculation is finally stored in another table (with just one row). Now I want to add this value to the starting table in the same row. And that is where I am stuck. I tried to work with SearchCursor and UpdateCursor but I am not sure how to make it work between these two tables. (Join is not possible, as there is no field the join could be based on)

Sorry, here is the script. The problem is really at the end.

import arcpy
from arcpy import env
from arcpy.sa import *

# Set the workspace
env.workspace = "D:/next/"
env.scratchWorkspace = "D:/next/"

# Set object property to overwrite existing output
arcpy.gp.overwriteOutput = True 
# Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial")

# Set Raster Variable
inRaster = arcpy.GetParameterAsText(0)
FieldName = arcpy.GetParameterAsText(1) 
inputFeature = arcpy.GetParameterAsText(2)    

#Add a field to the inputFeature, for results
arcpy.AddField_management(inputFeature, FieldName, "LONG")

#Create the search cursor
rows = arcpy.SearchCursor(inputFeature)

for row in rows:

    # Create a Where Clause to select only the current record
    whereClause = '"FID" = ' + str(row.FID)
    IDField = str(row.FID)
    tempData = "row" + IDField + ".shp"
    # Create a feature layer from the current row only
    arcpy.MakeFeatureLayer_management(inputFeature, tempData, whereClause)

    # Extract the cells of a raster that underly the selected feature
    maskedRaster = ExtractByMask(inRaster, tempData)
    maskedRaster.save ("D:/next/" + "row" + str(IDField))

    # Create the table for the new "maskedRaster" (for each value a row and the count of the values)
    newRaster = "row" + str(IDField)
    arcpy.BuildRasterAttributeTable_management(newRaster, "NONE")

    # here would be all the calculations
    # I leave them out the skript is long enough, already

    # Summarize the values. A dbf-table with only one row is the result
    stat = [["Calc", "SUM"]]
    arcpy.Statistics_analysis(newRaster, str(newRaster)+".dbf", stat )

    # Now create an update Cursor to add the value to the selected row
    rows2 = arcpy.UpdateCursor(inputFeature)

    # Here comes the problem
    for row2 in rows2: 
        if row2.FID == IDField:
            row2.setValue(FieldName, row2.getValue(arcpy.CalculateField_management(str(newRaster)+".dbf", "SUM_Calc"))

    del row2, rows2
del row, rows
  • 1
    Maybe you can add relevant parts of your script to the question, as it is now?
    – Martin
    May 16, 2013 at 10:57
  • I agree with @Martin - if we could see some of your (pseudo-)code it would be much easier to fill in the gaps than to write from scratch. If you are new to cursors you may be better to work with arcpy.Search/UpdateCursor before tackling the slightly harder arcpy.da.Search/UpdateCursor syntax.
    – PolyGeo
    May 16, 2013 at 11:13
  • Do you get an error (If so, post it)?. Or do you get unwanted output (blank, incorrect, etc)?
    – gm70560
    May 16, 2013 at 13:58
  • Data Access cursors (arcpy.da.SearchCursor,...) should be used instead of the clunky and slow original cursors.
    – Vince
    Sep 23, 2016 at 11:20

1 Answer 1


It looks like you are attempting two cursors on the same feature class at the same time. I am fairly certain that will not work.

From Accessing data using cursors, 10.0:

Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. The UpdateCursor or InsertCursor functions fail because of an exclusive lock on the dataset. If these functions successfully create a cursor, they apply an exclusive lock on the dataset so that two scripts cannot create an update or insert cursor on the same dataset.

If you have all your calculations stored by FID, finish the first cursor in the first loop and then start the second as a separate loop.

  • Thank you! that was indeed the problem. Although maybe not the only one!
    – user18205
    May 16, 2013 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.