[GIS] Why can’t I use SelectLayerByAttribute_management while a cursor is set

arcgis-10.0arcpycursorerror-000358select-by-attribute

I've been having a problem with arcpy in that the simple line:

arcpy.SelectLayerByAttribute_management ("TEMP_LINES")

produces this response: Runtime error : ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

I've worked out that this error only occurs when a cursor is set, like this:

lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i")

Can I not use select by attribute and the cursor or do I have one of them set up wrong?

EDIT: whole code:

arcpy.MakeFeatureLayer_management (Lines,"TEMP_LINES")

lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i")

for row in lineCursor:
    arcpy.SelectLayerByAttribute_management ("TEMP_LINES")
    arcpy.SelectLayerByLocation_management (Polygons, "INTERSECT", "TEMP_LINES")
    arcpy.FeatureClassToFeatureClass_conversion (Polygons, "temp_polys")
    polyCursor = arcpy.SearchCursor("temp_polys")
    blocks_list = []
    for row in cursor:
        blocks_list.append(row.getValue("BLOCK"))
    blocks_str = ', '.join(blocks_list)
    lineCursor.BLOCK = blocks_str   

Best Answer

As stated by others you have not provided a selection criteria for arcpy.SelectLayerByAttribute_management(). Additionally using arcpy.SelectLayerByAttribute_management() within an Update Cursor is only going to apply to a single row and does not really make sense to do.

If I understand what you are trying to accomplish, I think you need to rework your logic in this script. Perhaps run the arcpy.SelectLayerByLocation_management() previous to creating your cursors and use a where_clause as an argument to your arcpy.UpdateCursor to limit the number of records that you are searching and updating against.

Related Question