HOW TO
Fewer considerations are involved when using a Python script as a script tool in ArcMap, compared to using a Python script as a geoprocessing service published in ArcGIS Server. For example, when using the update cursor.
An update cursor in a geoprocessing script requires a feature class in a database location that must be updated manually. Otherwise, either all the values of an associated feature class in a choice list parameter referenced in the script during publishing are updated, or the service fails to update any feature at all because the input layer is a feature layer or feature set.
To update selected features using the update cursor, the script sets the workspace and the feature class name must be set as string data type. The strings are used in the update cursor in_table parameter. The following steps provided describe how to do so.
import sys, os, arcpy
workspace = arcpy.GetParameterAsText(0) fclassname = arcpy.GetParameterAsText(1)
fclass = r"{}\{}".format(workspace, fclassname)
flayer = arcpy.GetParameterAsText(2)
fields = ['UpdateField'] list = [ ]
with arcpy.da.SearchCursor(flayer, "OBJECTID") as cursor: for row in cursor: print (row[0]) list.append(row[0])
edit = arcpy.da.Editor(workspace) edit.startEditing(True, False)
strlist = "(" for num in list: strlist += str(num) + ", " strlist = strlist[:-2]+")"
with arcpy.da.UpdateCursor(fclass, fields, '"OBJECTID" IN {}'.format(strlist)) as cursor: for row in cursor: row[0] = "test" cursor.updateRow(row)
edit.stopEditing(True) print ("Done")
import sys, os, arcpy workspace = arcpy.GetParameterAsText(0) fclassname = arcpy.GetParameterAsText(1) fclass = r"{}\{}".format(workspace, fclassname) flayer = arcpy.GetParameterAsText(2) fields = ['UpdateField'] list = [ ] with arcpy.da.SearchCursor(flayer, "OBJECTID") as cursor: for row in cursor: print (row[0]) list.append(row[0]) edit = arcpy.da.Editor(workspace) edit.startEditing(True, False) strlist = "(" for num in list: strlist += str(num) + ", " strlist = strlist[:-2]+")" with arcpy.da.UpdateCursor(fclass, fields, '"OBJECTID" IN {}'.format(strlist)) as cursor: for row in cursor: row[0] = "test" cursor.updateRow(row) edit.stopEditing(True) print ("Done")
Get help from ArcGIS experts
Download the Esri Support App