2

This is my first time asking a question on StackExchange. I hope I phrase my question and describe my attempts to understand my problem well enough!

While working with SearchCursor, I'm trying to specify the SQL where_clause from a table called "Parcel" within the featureClass, and I want the MAX and MIN value from the column LAND_VALUE. Below is what I try just for MAX.

import arcpy

feature_class = "C:\foo.ext"
where_clause = "SELECT MAX(LAND_VALUE) AS MaxLandVal from Parcel"

rows = arcpy.SearchCursor(feature_class, where_clause, "", "", "LAND_VALUE D")

I'm told an invalid SQL statement was used. However, I'm not sure if the SQL statement is invalid, or if perhaps I'm stating the SQL query within SearchCursor incorrectly. Despite using the reference material for SearchCursor, SQL MAX() and SQL MIN(), and despite referencing a few different examples, I can't get this to work! Please advise!

Finally, how would I display MaxLandVal if the query were to be correct? Perhaps something like...?

for blah in rows:
    print blah.MaxLandVal

Thank you for your help!

  • Your question is phrased so that it is easy to understand and gives plenty of information about your problem. Welcome to SE! – Paul Jun 25 '13 at 4:05
3

When using SQL statements to restrict your cursors, you don't need to (and presumably can't) use the full syntax. From the help it mentions

SELECT * FROM forms the first part of the SQL expression and is automatically supplied for you.

For instance, if you wanted to select features from a shapefile with a FID > 1000, your SQL statement would be:"\"FID\">1000" (As when using it in Arcmap, the quotations around the field names are necessary, and therefore are escaped with the backslash character. You could also write it as """"FID">1000""", but it's really user preference. I prefer the former as it's easier for me to remember to escape each quotation mark rather than remember how many I need.)

In you case, I wouldn't bother with using SQL clause at all. Write all your values to a list and then use Python's built-in min() and max() functions to return the values.

If you are using ArcMap 10.1, I would recommend you switch to the data access module for your cursors as they are faster and handle releasing locks better than the original cursors. Syntactically, they are fairly similar.

  • 1
    You can also do '"FID" > 1000' Using ' around the string to avoid escaping. – Nathan W Jun 25 '13 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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