I'm trying to construct a where clause from user input that will eventually be passed through as a SQL statement in the select tool or the select by attributes tool. I've started a script thats been adapted from some previous posts, however, I still don't have it correct. the way its currently written the resulting SQL statement will look something like:
"123" = 'ABC' OR 'DEF'
I need the resulting statement to look more like
"\123\" = 'ABC' OR "\123\"='DEF'
Important: the values (i.e. ABC DEF) will be user inputs and the number of values entered as arguments may vary from user to user. My current script is below
def whereClause(table, field, values): """Takes a semicolon-delimited list of values and constructs a SQL WHERE clause to select those values within a given field and table.""" # Add field delimiters fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field) # Split multivalue at semicolons and strip quotes valueList = [value[1:-1] if (value.startswith("'") and value.endswith("'")) else value for value in values.split(';')] # Determine field type fieldType = arcpy.ListFields(table, field).type # Add single-quotes for string field values if str(fieldType) == 'String': valueList = ["'%s'" % value for value in valueList] # Format WHERE clause in the form of an IN statement whereClause = "%s = %s" % (fieldDelimited, 'OR' .join(valueList)) return whereClause def outName(input,post="Out",fileExtension="shp"): """Returns output name.""" outName=os.path.basename(input).split(".")+post+"."+fileExtension return outName # Create SQL Statement InputFC = arcpy.GetParametersAsText(0) outputFC=outName(InputFC,"_Select") Field ="CO_NAME" Values = arcpy.GetParameterAsText(1) #user input SQL=whereClause(InputFC,Field,Values) print SQL