2

This question already has an answer here:

Im trying to create a feature layer on which i want to perform a select by location, however:

arcpy.MakeFeatureLayer_management (perp, "selected","LINK_ID = 23002109")

throws:

ExecuteError: ERROR 000230: Failed selecting with LINK_ID = 23002109

and

arcpy.MakeFeatureLayer_management (perp, "selected")
arcpy.SelectLayerByAttribute_management("selected", "NEW_SELECTION", "LINK_ID = 23002109")

throws:

ExecuteError: ERROR 000358: Invalid expression

The same query works fine when done manually in ArcMap. The field exists and its datatype is DOUBLE.

My problem seems to be exactly the same as in ArcPy inconsistently accepts SQL queries for MakeFeatureLayer or SelectLayerByAttribute The difference is I'm working with a local GDB and not with a SDE connection. What is wrong here?

marked as duplicate by PolyGeo arcpy Nov 12 '17 at 21:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

To build SQL queries that will always work no matter input use AddFieldDelimiters:

Adds field delimiters to a field name to allow for use in SQL expressions.

The field delimiters used in an SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotation marks (" "), personal geodatabases use square brackets ([ ]), and enterprise geodatabases don't use field delimiters. The function can take away the guess work in ensuring that the field delimiters used with your SQL expression are the correct ones.

, Format String Syntax:

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.

And Three double quotes to build your query. Like this:

import arcpy
feature_class = r'C:\test.gdb\polygons'    
query = """{0} = {1}""".format(arcpy.AddFieldDelimiters(feature_class,'LINK_ID'), 23002109)
arcpy.MakeFeatureLayer_management(feature_class, 'polygon_lyr',query)

This query will also work in Select By Attributes

Not the answer you're looking for? Browse other questions tagged or ask your own question.