0

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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 8, 2019 at 8:22
2
  • Try: sql = "{0} LIKE '{1}'".format(arcpy.AddFieldDelimiters(datasource=lyr, field='COD_CABINA'), Cabina) and then arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause=sql) Commented Apr 8, 2019 at 8:55
  • I tried but does not work, appear the ERROR 000358. I tried to run the script on the Python idle and there works perfectly but if i run the Toolbox something go wrong. Actually the results of .format is '"COD_CABINA" LIKE \'A540\'' Commented Apr 8, 2019 at 11:31

2 Answers 2

1

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.

answered Apr 8, 2019 at 13:30
0

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.

answered Apr 8, 2019 at 13:26
1
  • Thank you so much to everyone it is work! now i will go deeper in the parameter Value! Commented Apr 8, 2019 at 15:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.