0

I am trying to create a script that will search for suitable site locations.

I want to let the user input an area size (e.g. >20) to be searched but each time I run the script it comes up as

Error 000358: Invalid expression. Failed to execute (SelectLayerByAttribute).

This section of the code looks like:

inputvalue =arcpy.GetParameterAsText(0)
query = "POLY_AREA = " + inputvalue
arcpy.SelectLayerByAttribute_management(featurelayer2, "NEW_SELECTION", query)
  • If you're evaluating a floating-point value for equivalence , it's unlikely to work correctly, but it won't fail by 000358, which means you have delimiter issues in addition to a bad example. – Vince Apr 30 at 15:45
1

Use AddFieldDelimiters to get correct syntax:

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

If POLY_AREA is a text/string field, you need to add single quotes around the {1}: '{1}'

import arcpy

featurelayer2 = 'somelayer'
inputvalue =arcpy.GetParameterAsText(0)

sql = "{0} = {1}".format(arcpy.AddFieldDelimiters(datasource=featurelayer2, field='POLY_AREA'), inputvalue)

arcpy.SelectLayerByAttribute_management(in_layer_or_view=featurelayer2, where_clause=sql) #You dont need "NEW SELECTION" part, this is default

If you want the user to input the operator and area (like: >30) try:

"{0} {1}".format(arcpy.AddFieldDelimiters(datasource=featurelayer2, field='POLY_AREA'), inputvalue)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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