1

In my script I am using the Select tool (arcpy.Select_analysis) and in the SQL expression, I want to use a number that I call in as a parameter. I have trying to script it like:

subsize = arcpy.GetParameter(3)

where_clause = '\"AreaSqKm\" > {0}'.format(subsize)

arcpy.Select_analysis(waterbodies, lg_waterbodies, where_clause)

In this example I'm entering the parameter as the number 0.0005, as a double.

I've tried tweaking it various ways, but every time I try to run the tool I get: ExecuteError: ERROR 000358" Invalid expression "AreaSqKm > 0.0005 Failed to execute (Select).

If i hard code that variable in a script as 0.0005 and run it as a stand alone it works fine. Any ideas on why it won't run in arc?

1
  • Is waterbodies and lg_waterbodies shapefile,arcsde,geodatabase etc. please refer different where_clause for each type..
    – Learner
    Jan 31, 2015 at 7:52

2 Answers 2

1

You do not need the escaping backslash ('\"') since you are using single quotes to enclose the whole string. This should work.

where_clause = '"AreaSqKm" > {0}'.format(subsize)
1
  • Yeah, I have tried this, I think the problem is the way the parameter is being dealt with. I need it to be treated as a number for the SQL expression to work, and it seems like that's where the problem is. Thanks! Jan 30, 2015 at 20:17
0

The double quotes indicate your field name and should not be escaped with a backslash. Try this instead:

where_clause = '"AreaSqKm" > {0}'.format(subsize)

To test that works I created the test code and tool dialog below, and it worked. I reported a couple of variable values using arcpy.AddMessage() to ensure that they were actually set to what I thought they were. You have not specified where your data is stored. In my test I used a shapefile.

import arcpy

arcpy.env.overwriteOutput = True

subsize = arcpy.GetParameterAsText(0)
arcpy.AddMessage("subsize: {0}".format(subsize))

where_clause = '"AreaSqKm" > {0}'.format(subsize)
arcpy.AddMessage("where_clause: {0}".format(where_clause))

arcpy.Select_analysis("C:/polygeo/test.shp","C:/polygeo/TestSelect.shp",where_clause)

enter image description here

7
  • Yeah, I have tried this, I think the problem is the way the parameter is being dealt with. I need it to be treated as a number for the SQL expression to work, and it seems like that's where the problem is. Thanks! Jan 30, 2015 at 20:17
  • Can you edit your question to indicate where your data is stored as well as the exact error thrown when this syntax is used, please?
    – PolyGeo
    Jan 30, 2015 at 20:20
  • Ok, so I'm also selecting the workspace using arcpy.GetParameterAsText as setting it to a folder where a shapefile (waterbodies.shp) is stored. I am trying to select waterbodies larger than a given threshold (0.0005 km^2) in this case. So I use arcpy.Select_analysis and the SQL expression as shown before. I then get this error: ExecuteError: ERROR 000358: Invalid expression "AreaSqKm" > 0.0005 Failed to execute (Select). I need the threshold value to be a modifiable number so I can't hard code it into the script. Jan 30, 2015 at 20:36
  • I cannot test at the moment but try GetParameterAsText instead of GetParameter.
    – PolyGeo
    Jan 30, 2015 at 20:44
  • I actually have tried that as well, doesn't seem to solve anything... Jan 30, 2015 at 20:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.