I am troubles using the SelectLayerByAttribute() function for arcgis geoprocessing.

I have a layer and I want to select everything that matches on a particular attribute. I call MakeFeatureLayer() to create a layer, I run SelectLayerByAttribute() to select what I want, and then I run CopyFeatures() to save that layer into the database.

However, after the copyfeatures() call, nothing is saved and I have empty layers.

Is there something I'm missing?

I'm using python and arcgisscripting module for geoprocessing. My code below:

 gp.MakeFeatureLayer_management(target_layer, "lyr") 
 gp.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", "\"my_attribute\" = 5 " )

 # Write the selected features to a new featureclass
 gp.CopyFeatures_management("lyr", output)
  • The basics looks good, I would expect it to work. To really get your script "perfect", I would recommend using ModelBuilder to drag/drop on your tools, make sure it works as expected, then export to Python which will build the correct code for you. – RyanDalton Apr 13 '11 at 5:58

I did it this way:

whereClause = '"ID" = ' + str(iID) + ' AND "Level" = ' + str(iAlt) + ' AND "Time" = ' 
              + str(iTime)

arcpy.MakeFeatureLayer_management(SelectFromDataset, "SelecttoLayer", whereClause)

And it works fine


you got it all correct, should work.

if this was run as a script tool, i'd say check your "extent" environment... but since this is a script, i'd tack these 2 lines at the end of your script to try to pinpoint where 0 records start

for ds in [target_layer, 'lyr', output]:
  print('{0} has {1} records in it'.format(ds, gp.GetCount_management(ds)))

You could just use the gp.FeatureClasstoFeatureClass_conversion method. It'll save you from creating a layer and selecting on it - you just set a where clause in the method call.

As for you current dilemma, check your where clause - remember that the field 'identifiers' can can be different if its a personal geodatabase - square brackets [] instead of quotes. And if your field is a string field with a number in it, you'll need to put quotes around it as well.

You might also use single quotes for your python strings so you don't have to escape your double quotes - it makes it look nicer, but won't solve your problem.


Looks like my script works as intended. I didn't realize that the fields in the queries are case sensitive... I used the incorrect casing for the field name and ArcGIS didn't throw an error about it.

I made this poor assumption because our data sits on Oracle databases, which are configured to be case-insensitive.

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.