parameter in arcpy

2030
4
Jump to solution
04-10-2016 10:18 PM
jayasudha
New Contributor III

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' ?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AndrewKeith3
Occasional Contributor

arcpy.MakeFeatureLayer_management("C:/test.gdb/cities","cities_lyr", """ "STATE_NAME" = '{0}' """.format(x))

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

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

AndrewKeith3
Occasional Contributor

arcpy.MakeFeatureLayer_management("C:/test.gdb/cities","cities_lyr", """ "STATE_NAME" = '{0}' """.format(x))

DavidWasserman
Regular Contributor

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))
David Wasserman, AICP
jayasudha
New Contributor III

Thanks to all for your suggestions.

0 Kudos