I am trying to create a .pyt that find and select an object in an specific layer but I have a problem. I'm working on the Desktop version 10.5 and actually the code is this one:
def getParameterInfo(self):
"""Define parameter definitions"""
Cabina = arcpy.Parameter(
displayName="Cabina",
name="cab",
datatype="string",
parameterType="Required",
direction="Input")
params = [Cabina]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.Layer(r"ESERCIZIO\ELE_ESER_Cab")
exp = "'" + str(Cabina) + "'"
arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause= "COD_CABINA LIKE" + exp)
arcpy.RefreshActiveView()
return
`
the problem is that continue to appear this error message: ERROR 000358: Invalid expression Failed to execute. (I'm working in an Oracle environment) Actually I tried to put a value such as exp = 'A0001' and the script work well. I read almost everything but seems like nothing works. I tried to find some documentation about how to use custom input and SQL but I did not find so much.
2 Answers 2
If you change your code to this, it should work:
def getParameterInfo(self):
"""Define parameter definitions"""
Cabina = arcpy.Parameter(
displayName="Cabina",
name="cab",
datatype="string",
parameterType="Required",
direction="Input")
params = [Cabina]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.Layer(r"ESERCIZIO\ELE_ESER_Cab")
Cabina = parameters[0].valueAsText
exp = "{} LIKE '{}'".format(arcpy.AddFieldDelimiters(datasource=lyr, field='COD_CABINA'), Cabina)
arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause= "COD_CABINA LIKE" + exp)
arcpy.RefreshActiveView()
return
The execute method doesn't know anything about the parameter you defined as Cabina, you need to define this again within this function.
You are defining Cabina
as a Parameter then treating it as if it is a string variable when you build your expression. You need to access the Parameters value and build the sql expression from that.
-
Thank you so much to everyone it is work! now i will go deeper in the parameter Value!vinjazz– vinjazz2019年04月08日 15:43:57 +00:00Commented Apr 8, 2019 at 15:43
Explore related questions
See similar questions with these tags.
sql = "{0} LIKE '{1}'".format(arcpy.AddFieldDelimiters(datasource=lyr, field='COD_CABINA'), Cabina)
and thenarcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause=sql)