1

I get this error when 'arcpy.SelectLayerByAttribute_management' statement is executed. I tried many different ways to format my whereclause. I had printed it and it looks fine. But the error still there.

Anyone had similar problem?

fc_layer is a feature layer, that created in the code from a feature class in file geodatabase.

Runtime error Traceback (most recent call last): File "", line 6, in File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 7221, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

My code:

field1 = ['OBJECTID']
with arcpy.da.UpdateCursor('fc_layer', field1) as cursor:
     for row in cursor:
         currentID = row[0]
         whereClause1 = ''' "OBJECTID" = {0} '''.format(currentID) 
         arcpy.SelectLayerByAttribute_management("fc_layer", "NEW_SELECTION", whereClause1)
         arcpy.SelectLayerByLocation_management("fc_layer", "BOUNDARY_TOUCHES", "fc_layer", "","ADD_TO_SELECTION", "NOT_INVERT")  

2 Answers 2

1

Try this:

field1 = '[OBJECTID]'
with arcpy.da.UpdateCursor('fc_layer', "OBJECTID") as cursor:
     for row in cursor:
         currentID = row[0]
         whereClause1 = field1 + '=' + currentID 
         arcpy.SelectLayerByAttribute_management("fc_layer", "NEW_SELECTION", whereClause1)
         arcpy.SelectLayerByLocation_management("fc_layer", "BOUNDARY_TOUCHES", "fc_layer", "","ADD_TO_SELECTION", "NOT_INVERT")  

With file geodatabases, the field name is called as [field], not "field" as with shapefiles.

1

I think this should work:

field1 = 'OBJECTID'
with arcpy.da.UpdateCursor('fc_layer', field1) as cursor:
     for row in cursor:
         currentID = row[0]
         whereClause1 = "{0} = {1}".format(field1,currentID) 
         arcpy.SelectLayerByAttribute_management("fc_layer", "NEW_SELECTION", whereClause1)
         arcpy.SelectLayerByLocation_management("fc_layer", "BOUNDARY_TOUCHES", "fc_layer", "","ADD_TO_SELECTION", "NOT_INVERT")  
2
  • Thanks for your reply. The script worked. I had to replace UpdateCursor with SearchCursor. If I use UpdateCursor, I still get the same error message.
    – lida
    Commented Oct 24, 2016 at 23:07
  • If that is all that you are doing with the rows in your cursor then it would make sense to use a read-only SearchCursor rather than an UpdateCursor because you appear to be doing no updating.
    – PolyGeo
    Commented Oct 24, 2016 at 23:12

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.