1

This question already has an answer here:

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.

marked as duplicate by PolyGeo arcpy Nov 9 '16 at 23:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What is the full error message including line number? Can you always supply error messages as text rather than pictures, please? – PolyGeo Nov 9 '16 at 22:44
  • Sorry about that. I will learn how to show error message with the code. – Tianxin Li Nov 9 '16 at 22:49
  • See also this question which also constructs a where clause including wildcards. – JasonInVegas Nov 9 '16 at 22:52
  • All you need to do is click in that results window and Ctrl-A, then Ctrl-C then Ctrl-V to select all, copy and paste. – PolyGeo Nov 9 '16 at 22:56
3

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

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