AnsweredAssumed Answered

Select Layer By Attribute User Input

Question asked by scottaidh on Jan 8, 2013
Latest reply on Jan 9, 2013 by scottaidh
I have been puzzling over a script I have to write and run in an Arc toolbox and I wondered if anyone could tell me where I'm going wrong.

The following code relates to a rivers shapefile called Rio_Panuco in a geodatabase called panuco.gdb.  There is a field in the shape file called "type" and the attribute I want to select is "link".

I was able to create the file I wanted when the script was hard coded to the geodatabase using the code below-
arcpy.SelectLayerByAttribute_management("River_lyr", "NEW_SELECTION", '"type" = \'link\'')

However, I need to write generic code and require the user to input the requirements i.e. Select * from Rio_Panuco where "type" = 'link'

I have written the following code to do this (and variations galore!) but still cannot get it to work properly. I've also put the code in ArcToolbox but when the Select Layer By Attribute box is clicked it is blank.  I would be grateful for any advice.

# extract features by attrtibute

# import system modules
import arcpy
from arcpy import env

# get user supplied path, layers and fields
path is workspace
path = arcpy.GetParameterAsText(0)

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = '"' str(typeField) + " = '" + str(confluence) + "'"
rows = arcpy.SearchCursor(inputLayer, whereClause)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText()

# set overwrite option
# arcp.overwriteOutput = true

# error trapping measures
try:
# make a layer from the feature class
arcpy.MakeFeatureLayer_management(inputLayer, "River_lyr")

# select layer by attribute
arcpy.SelectLayerByAttribute_management("River_lyr", "SelectionRiverLayer", whereClause)

# write selected features to a new featureclass
arcpy.CopyFeatures_management("River_lyr", "SelectionRiverLayer")

except:
print arcpy.GetMessages()

Outcomes