Python Tool - User input then Select by Attribute

3372
21
02-06-2017 01:22 PM
DevinUnderwood2
Occasional Contributor

Any ideas why I am having problems by trying to have user input a string value for a feature, then have the feature selected. 

#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
#os.chdir(r'C:\Users\dunderwood\Documents\MY_TEMPLATES\WASTEWATER_EDIT_TEMPLATE')
# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

whereClause = FACILITYID == InputValue"
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereClause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

21 Replies
DarrenWiens2
MVP Honored Contributor

The problem is your where clause. See here for how to specify a query in Python, especially the last example: Specifying a query in Python—ArcPy Get Started | ArcGIS Desktop 

e.g.

whereclause = """{} = 2""".format(arcpy.AddFieldDelimiters(fc, fieldname))
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Select analysis has a code snippet and links to formulating sql queries that will work.  Your query lacks the proper quotation sequence

0 Kudos
DevinUnderwood2
Occasional Contributor

Thank you for the quick responses.

I will work on this, at first glance, I need brackets and/or add field delimiter, etc. I am taking away from this that my sql clause needs work.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I believe you can just use this for your case:

whereClause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass, 'FACILITYID'),InputValue)
0 Kudos
DevinUnderwood2
Occasional Contributor

Does it need 'FACILITYID' since it is referenced in InputValue ?

I tried this line and still having difficulty.  

0 Kudos
DarrenWiens2
MVP Honored Contributor

Oh, right. It should be like below so that all three input variables are dynamic.

whereClause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass, InputField),InputValue)
0 Kudos
DevinUnderwood2
Occasional Contributor

I get the following message, don't know what is wrong.

Executing: QueryMxdFeatureClass "SWR Layers\Manholes" FACILITYID 5564

Start Time: Mon Feb 06 15:41:32 2017

Running script QueryMxdFeatureClass...

Failed script QueryMxdFeatureClass...

SyntaxError: invalid syntax (QueryMxdFeatureClass.py, line 19)

Failed to execute (QueryMxdFeatureClass).

Failed at Mon Feb 06 15:41:33 2017 (Elapsed Time: 0.67 seconds)

Here is my updated syntax

#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField,InputValue)

arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereclause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Can you put in some print statements so that the exact format can be examined.  If the input value is a number there is one format, if the input value there is another... plus Darren updated his where clause with a syntax different thatn you provided on line 10

0 Kudos
DevinUnderwood2
Occasional Contributor

Oh didn't catch that. I changed it.  I am unable to use print statements to test it out since I am running it as a tool which doesn't return print statements.

whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
0 Kudos