ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

356
2
Jump to solution
02-05-2021 07:18 AM
CliveSwan
Occasional Contributor II

Greetings,

I have a query reading the Shape field, to return the number of records.

 

The code is:

    for ds in datasets:
        for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
            cursor = arcpy.SearchCursor(fc)
            new_name = fc.split('.')[-1]
            #print(new_name)

            #path = os.path.join(arcpy.env.workspace, ds, fc)
            #### PRINT full name
            rowitem = ([fc])
            #### PRINT Feature name
            #rowitem = ([new_name])
            print(rowitem)
            #Count_feat = arcpy.GetCount_management(rowitem.objectid) 
            query = "SELECT '*' FROM 'Shape'"
            #print(rowitem, query)
            arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", query ) 
            Count_feat = arcpy.GetCount_management(fc)
            print(Count_feat)

 

The error message is:

START
LOOP
['server.gis.Rail_Systems']

Then error:

ExecuteError                              Traceback (most recent call last)
<ipython-input-24-43e73deac686> in <module>
     47             query = "SELECT '*' FROM 'Shape'"
     48             #print(rowitem, query)
---> 49             arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", query )
     50             Count_feat = arcpy.GetCount_management(fc)
     51             print(Count_feat)

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py in SelectLayerByAttribute(in_layer_or_view, selection_type, where_clause, invert_where_clause)
   8752         return retval
   8753     except Exception as e:
-> 8754         raise e
   8755 
   8756 @gptooldoc('SelectLayerByLocation_management', None)

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py in SelectLayerByAttribute(in_layer_or_view, selection_type, where_clause, invert_where_clause)
   8749     from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject
   8750     try:
-> 8751         retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
   8752         return retval
   8753     except Exception as e:

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py in <lambda>(*args)
    509         val = getattr(self._gp, attr)
    510         if callable(val):
--> 511             return lambda *args: val(*gp_fixargs(args, True))
    512         else:
    513             return convertArcObjectToPythonObject(val)

ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

 

I would appreciate any pointers.

Regards,

Clive

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

The where_clause parameter is only that, the where clause portion of a full select statement. The fields and the table are implied with the other parameters. It looks like your intention is to select everything? Try this trick instead:

query = "1=1"

If you do intend to select something specific (instead of all records), just use the where clause, like:

query = "someNameField = 'example' or someNumberField > 0"

View solution in original post

2 Replies
BlakeTerhune
MVP Regular Contributor

The where_clause parameter is only that, the where clause portion of a full select statement. The fields and the table are implied with the other parameters. It looks like your intention is to select everything? Try this trick instead:

query = "1=1"

If you do intend to select something specific (instead of all records), just use the where clause, like:

query = "someNameField = 'example' or someNumberField > 0"

View solution in original post

JoeBorgione
MVP Esteemed Contributor

I don't notice  make feature layer anywhere.

can't wait to retire....