where_clause = ("Status = 'U'") arcpy.MakeFeatureLayer_management("Crimes", "Crimes_Lyr") arcpy.SelectLayerByAttribute_management("Crimes_Lyr", "NEW_SELECTION", where_clause) print arcpy.GetCount_management("Crimes_Lyr")
fld = arcpy.AddFieldDelimiters("Crimes", "Status") where_clause = (fld + " = 'U'") arcpy.MakeFeatureLayer_management("Crimes", "Crimes_Lyr") arcpy.SelectLayerByAttribute_management("Crimes_Lyr", "NEW_SELECTION", where_clause) print arcpy.GetCount_management("Crimes_Lyr")
Writing a python script and attempting to use the SelectLayerByAttribute_management command.
I've tried it several different ways and keep getting a syntax error on the variable set, or if the variable set works, get invalid expression. I am querying against a file geodatabase.
python snippet:
where_clause = (r"Status = 'U'")
arcpy.SelectLayerByAttribute_management("Crimes", "NEW_SELECTION", where_clause)
I'd like to add .outputCount to this to see if there are any rows selected. I have done this once in the python window but can't remember how I did it now and didn't put it in my script when it worked.
I've also tried various ways of putting quotes and single quotes with and without the r or the parenthesis. Syntax error or it doesn't get passed properly to the query and gives the Invalid expression.
Help!
Forgot to add: In the ArcMap window using the Select by Attributes dialog, the proper syntax is "Status" = 'U' and it works as expected. Just can't get the translation to python to work properly.
arcpy.SelectLayerByAttribute_management("Crimes", "NEW_SELECTION", "\"Status\"='U'")
whereClause = "ADDRNO = 1930 AND ADDRSTREET = 'BIG OWL' AND ADDRSUFFIX = 'ROAD'"
Thanks for the suggestion. When I tried it, I got a new error:
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000825: The value is not a layer or table view
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).
Failed to execute (TestAddressZoomTo).
Any other suggestions? Again, I am trying to select 3 address fields and zoom to the selected feature. The first field is numeric, the other 2 fields are text. I also tried changing the script so that the 3 fields were user defined parameters. Then I used the GetParameterAsText function within the SelectLayerByAttribute where clause. I got the same error as listed above. Here is the revised script I was trying:
# Script arguments
PAR1 = arcpy.GetParameterAsText(0)
PAR2 = arcpy.GetParameterAsText(1)
PAR3 = arcpy.GetParameterAsText(2)
#Select by Attributes
parcels = "C:\PreApp Maps\Parcels.lyr"
whereClause = "ADDRNO = PAR1 AND ADDRSTREET = PAR2 AND ADDRSUFFIX = PAR3"
arcpy.SelectLayerByAttribute_management (parcels, "NEW_SELECTION", whereClause)
#Zoom to Selected
mxd = arcpy.mapping.MapDocument(r"Current")
df = arcpy.mapping.ListDataFrames(mxd, "Boulder County")[0]
df.zoomToSelectedFeatures()
#arcpy.RefreshActiveView()
arcpy.env.workspace ="C:\PreApp Maps" arcpy.MakeFeatureLayer_management("Parcels", "lyr") ... arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION", whereClause)