I am trying to create a Python-Toolbox with following Tool:
- inputFeature: A User has to load a Feature
- inputField: Then he has to to select a field from the Input Feature
- inputValue: At the end he has to put in a value (no matter which datatype: date, string, numeric...)
The execute method now should select all rows which are equal to the inputValue.
I already had a working code, but just for numeric inputValues. Another User recomended this code form a past post:
import arcpy
def buildWhereClause(table, field, value):
"""Constructs a SQL WHERE clause to select rows having the specified value
within a given field and table."""
# Add DBMS-specific field delimiters
fieldDelimited = arcpy.AddFieldDelimiters(table, field)
# Determine field type
fieldType = arcpy.ListFields(table, field)[0].type
# Add single-quotes for string field values
if str(fieldType) == 'String':
value = "'%s'" % value
# Format WHERE clause
whereClause = "%s = %s" % (fieldDelimited, value)
return whereClause
if __name__ == "__main__":
inputfc = r"C:\input.shp"
outputfc = r"C:\output.shp"
fieldname = "StudyID"
fieldvalue = 101
whereclause = buildWhereClause(inputfc, fieldname, fieldvalue)
arcpy.Select_analysis(inputfc, outputfc, whereclause)
This is the link to the post: Including variable in where clause of arcpy.Select_analysis() It sounds that this would be the perfect solution. But I have no idea how to implement the method in my Python-Toolbox and how to run it with Input-Parameters. Does somebody know how to put the code into the standard Python-Toolbox Codeblock? I mean shouldn't it be used in def execute?