Wednesday 28 December 2016

arcgis 10.0 - Including variable in where clause of arcpy.Select_analysis()?


I am trying to loop through a shapefile, selecting each feature in turn and copying it to a temporary shapefile to by included in a union analysis. I'm using a cursor to find the ID name for each feature which I'm setting to a varible 'Name'. Whenever I try to use this variable as part of the where clause in arcpy.Select_analysis I get an error:


ExecuteError: ERROR 999999: Error executing function. An invalid SQL statement was used. An invalid SQL statement was used. Failed to execute (Select).


The code I'm using is:


Name = 101
where = "\'\"StudyID\" = \\'"+str(Name)+"\\'\'"

arcpy.Select_analysis("C:\\input.shp", "C:\\output.shp", where)

If I type it out without using the variables:


arcpy.Select_analysis("C:\\input.shp", "C:\\output.shp", '"StudyID" = \'101\'')

it works fine


What to I need to do to fit the variable into the sql statement?



Answer



Another, maybe simpler, way is:


where = '"StudyID" = ' + "'%s'" %Name

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...