2

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 
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Dec 13, 2013 at 10:54
5
  • 1
    You don't provide a valid expression for the Select Layer by Attribute GP tool. You only provide the layer name which is not enough. Commented Dec 13, 2013 at 11:09
  • 1
    without the cursor on this selects all. It doesnt work with any expression with the cursor on Commented Dec 13, 2013 at 11:17
  • Can you provide the whole chunk of code you execute please? I am a bit confused in which sequence you have those rows. Commented Dec 13, 2013 at 11:33
  • @AlexTereshenkov I've edited to include Commented Dec 13, 2013 at 11:48
  • i've tested your code succefully, without any problems, it seems that the problem is not located in SelectLayerByAttribute_management Commented Dec 13, 2013 at 16:48

2 Answers 2

2

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.

answered May 5, 2014 at 22:04
1

The SelectLayerByAttribute_management tool in your script is trying to select all features within the feature layer, whereas the cursor accesses one row at a time within the feature. If you would still like to use the tool within the cursor then there is a bit of an adjustment needed. I should make mention that this adjustment assumes the field "combined_i" is not unique.

lineCursor = arcpy.UpdateCursor("TEMP_LINES","","","combined_i; unique_field")
cols = arcpy.ListFields("TEMP_LINES")
for row in lineCursor:
 for col in cols:
 if col.name == "unique_field":
 objstr = row.getValue(col.name)
 arcpy.SelectLayerByAttribute_management("TEMP_LINES", "NEW_SELECTION", ("%s = %s" % (col.name, objstr)))
 .
 .
 .

Allow me to explain the additional code in more detail:

cols = arcpy.ListFields("TEMP_LINES")
 for col in cols:
 if col.name == "unique_field":
 objstr = row.getValue(col.name)

First, list all fields in "TEMP_LINES", then loop through the fields and find a field named "unique_field". If true then set objstr equal to the value of "unique_field" in the current row.

 arcpy.SelectLayerByAttribute_management ("TEMP_LINES", "NEW_SELECTION", ("%s = %s" % (col.name, objstr)))

Instead of trying to select all features in "TEMP_LINES" this will only select the feature with the unique value in "unique_field" at the current row.

answered Dec 16, 2013 at 22:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.