I am trying to create python script tool which takes parameter.
x = arcpy.GetParameterAsText(0)
arcpy.MakeFeatureLayer_management("C:/test.gdb/cities","cities_lyr", """ "STATE_NAME" = 'Idaho' """)
How to pass value of x variable for STATE_NAME instead of constant value 'Idaho' ?
Solved! Go to Solution.
arcpy.MakeFeatureLayer_management("C:/test.gdb/cities","cities_lyr", """ "STATE_NAME" = '{0}' """.format(x))
state_name = arcpy.GetParameterAsText(1) # just get the sql query formulated correctly or define this parameter as an SQL in the tool in the parameter type
arcpy.MakeFeatureLayer_management("C:/test.gdb/cities","cities_lyr", """ "STATE_NAME" = '{0}' """.format(x))
I would also consider using the "Add Field Delimiters" function. It makes sure you have appropriate delimiters for your SQL queries.
AddFieldDelimiters—ArcPy Functions | ArcGIS for Desktop
Example:
GeodesignToolkit-GIS-CityEngine-Integration-Tools/SplitFeaturebyAttribute.py at master · Holisticnat...
def constructSQLEqualityQuery(fieldName, value, dataSource): # Creates a workspace sensitive equality query to be used in arcpy. return "{0} = '{1}'".format(arcpy.AddFieldDelimiters(dataSource, fieldName), str(value))
Thanks to all for your suggestions.