More

Can't get the where clause in Select layer by attribute working!

Can't get the where clause in Select layer by attribute working!


I am trying to develop a script that searches for a particular ID in a user selected field. To this end I have used the select layer by attributes tool to search for a derived number in a field specified by the user.I cannot, however, after numerous attempts, get the where clause right. I have posted my latest attempt below:

import arcpy from arcpy import env work_space = "L:DRAINAGED_GROUP6STAFFCJEEDRNbEDRN_holding.gdb" env.workspace = work_space arcpy.env.overwriteOutput = True selected_li = "L:DRAINAGED_GROUP6STAFFCJEEDRN_holding.gdbEDRN7EDRN_LINK" OB_ID = "OBJECTID_1" rows = arcpy.SearchCursor(selected_li) for row in rows: id_LCC = row.getValue(OB_ID) id_TNDE = row.getValue("TO_DRNNODE") if id_TNDE == " " or id_TNDE == None: arcpy.MakeFeatureLayer_management(selected_li, "feature_layer10") arcpy.SelectLayerByAttribute_management ("feature_layer10", "NEW_SELECTION", '"' + str(OB_ID) + '" =' + "'" + str(id_LCC) + "'")

So, if anyone has any ideas please do let me know.


If we print out your formatted string we get this: '"OB_ID" ='id_LCC"

This is not what you want. Format considerations for file GDBS are as follows:

  • Field names are not qouted
  • If the value is a string, use single quotes
  • If the value is a number, do not use quotes
  • Review the help docs for more details

Use .format to simplify the variable substitution

The query format should be:

"{} = '{}'".format(OB_ID,ID_LCC)

Watch the video: Dont Use Excel Filters! Use This Incredible Excel Formula Instead..