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.

1
  • 2
    SQL requires string constants to have single quotes about them.
    – Vince
    Jan 15, 2016 at 2:46

1 Answer 1

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()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.