I am developing a Python script, in which I want to select features based on an attribute, which is a string. I tried for hours and used a hundred different ways, but I always get the Error 000358 that the query statement is not correct.
I work with a feature class, not shapefile. Assume that my field is called "Fruit" and the value I want to select is "Apple". What I already tried:
whereClause = """"{} = {}""".format("Fruit", "'Apple'")
whereClause = """"{} = '{}'""".format("Fruit", "Apple")
whereClause = """"{0} = '{1}'""".format("Fruit", "Apple")
fruit = "Fruit"
value = "Apple"
whereClause = """"{} = '{}'""".format(fruit, value)
arcpy.management.SelectLayerByAttribute(myFeatureClass, "NEW_SELECTION", whereClause)
I also did some other Select By Attribute queries, but always using Integer or other numeric fields and I never had problems. Seems that I cannot get it to work with strings.
The error message I get is:
Traceback (most recent call last):
File "<string>", line 718, in execute
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8754, in SelectLayerByAttribute
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8751, in SelectLayerByAttribute
retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression. Error with executing of SelectLayerByAttribute.