Friday, 29 July 2016

arcpy - What is the right SQL query for this case?




I want to use input string in part of the SQL query, and the code has been posted below.


import arcpy
import os

in_features = arcpy.GetParameterAsText(0)
target_county = arcpy.GetParameterAsText(1)
out_path = os.path.dirname(in_features)
out_name= "%s.shp" % target_county
where_clause=""""CNTY_NM" = %s""" % target_county

arcpy.FeatureClassToFeatureClass_conversion(in_features, out_path, out_name, where_clause)

When I run the script, it gives the error. enter image description here enter image description here


I don't know what is wrong with the code. Any suggestions?


I am using ArcGIS 10.3.1 Desktop Advanced License.



Answer



In a SQL where clause, single quotes ("FIELD" = 'provided_value') are needed around the match string in the where clause. You are missing the single quotes around the provided value. It should be like this:


where_clause="\"CNTY_NM\" = '%s'" % target_county

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...