0

I am having problems writing WhereClause for my program. I am using 10.3 arcmap. My layer comes form a file geodatabase.

I have this code:

mlayer = arcpy.mapping.ListLayers(mxd, "1415_LGA_SpatJ", df)[0]
fn_field = 'MAPID'

....function which creates list of values a_list

for each_a in a_list:
    whereClause = '"%s" = %s'%(fn_field, each_a)
    arcpy.SelectLayerByAttribute_management(mlayer, "NEW_SELECTION", whereClause)

....

This code works when values of my field are numbers (field is long integral)

Now I need to modify the code and use another field instead, which has strings. I get an error, invalid sql expression.

Could someone please help with updating sql statement.

I tried few things, but don't understand it really well.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
lida
  • 147
  • 13

1 Answers1

1

Try this:

mlayer = arcpy.mapping.ListLayers(mxd, "1415_LGA_SpatJ", df)[0]
fn_field = 'MAPID'

....function which creates list of values a_list

for each_a in a_list:
    whereClause = '"' + fn_field + '"' + ' = ' + "'" +  each_a + "'"
    arcpy.SelectLayerByAttribute_management(mlayer, "NEW_SELECTION", whereClause)

Assuming it works it represents just another variation on Including variable in where clause of arcpy.Select_analysis()?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338