-1

What is wrong with this code?

query='"depth">1700 AND "depth"<1750'

with arcpy.da.UpdateCursor(out_layer,"depth",query)as cursor:
    for row in cursor:
        row[0]=0
        cursor.updateRow(row)

My query is not updated with the new value.

2
  • getting error RuntimeError: create cursor has failed
    – Mukesh
    Commented Nov 26, 2015 at 8:57
  • Welcome to GIS SE! As a new use please take the Tour to learn about our focussed Q&A format. Here your question body is "code only" with no indication of what it is supposed to do, and an error message only revealed in a comment.
    – PolyGeo
    Commented Nov 26, 2015 at 10:09

2 Answers 2

2

I'd suggest using the AddFieldDelimiters function. This allows you to enter a field name and a data source, and the field delimiters are automatically added.

field = arcpy.AddFieldDelimiters(out_layer, "depth")
query = """{0} > 1700 AND {0} < 1750""".format(field)

with arcpy.da.UpdateCursor(out_layer, "depth", query) as cursor:
    for row in cursor:
        row[0] = 0
        cursor.updateRow(row)

This is assuming out_layer is a valid layer.

1

You don't need double quotes around the field names. If you depth field is numeric, use this:

query = '''depth > 1700 AND depth < 1750'''

Also, enclosing the query with triple quotes is a good way to make sure the quotes delimiting the query won't be matched with the quotes delimiting field values within the query.

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