2

I'm trying to select features from a layer that I know exist. I'm using SelectLayerByAttribute_management in arcpy. I have a layer that is properly defined earlier in the code named "tourmaline". I'm trying to query this layer to return all features with value of "Occurrence" in the field STATUS__NEW.

This is what I have:

where = "Status__New = 'Occurrence'" 
arcpy.SelectLayerByAttribute_management(tourmaline, "", where)

No features on the map are selected when this code is run. I've tried variations of the where clause using double quotes and square brackets around field name, but none of them work.

Using ArcMap's Select Layer By Attributes tool to select the same features using the following SQL statement selects the features correctly.

SELECT * FROM tourmaline WHERE:
Status__New = 'Occurrence'

Why is this happening? Why is my SelectLayerByAttribute statement wrong?

  • Does your field name really have two underscores? Status_New instead of Status__New ? – Kirk Kuykendall Feb 8 at 21:05
  • Yes, the field name has two underscores. – cesium133 Feb 8 at 21:07
  • Please edit the question to specify the type of the data source (different storage types have different formatting requirements). – Vince Feb 9 at 14:48
  • Try replacing Status__New with [Status__New]. – cbunn Feb 12 at 0:39
  • 1
    Turns out I did not properly define my tourmaline layer. I used arcpy.mapping.Layer(r"C:\tourmaline.lyr") to define it. I should've gone the route of using ListLayers and indexing to point the layer at a variable and then call that variable. – cesium133 Feb 12 at 20:15
1

As long as your field name is correct (not one or three underscores instead of the two you show), it should work. You have it formatted correctly. Have you checked that there isn't a difference between the field name and alias on this field? Arcpy reads the true field name for this tool.

The only other thing I can think of is that if your Status__New field has coded domain values, you need to be selecting by the coded value. For instance, if there were a coded value of 1 for "Occurrence", you'd want to use the following selection:

    where = "Status__New = 1"
  • Turns out I did not properly define my tourmaline layer. I used arcpy.mapping.Layer(r"C:\path\tourmaline.lyr") to define it. I should've gone the route of using ListLayers and indexing to point the layer at a variable and then call that variable. Learned something new. – cesium133 Feb 12 at 20:16
  • Glad you figured that out! So the query works now? – flintlockspecial Feb 12 at 20:23
  • Yep! Works like it's supposed to. Thanks for the help! – cesium133 Feb 13 at 17:03

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.