2

Why is row not recognized within my update cursor?

The data is stored in a file geodatabase.
I am using ArcGIS Desktop version 10.1 ArcInfo License.

The screen shot shows the data "ORDER BY X ASC" with some values assigned.
Now I'm trying to reverse sort and continue adding values in the last column.

I know cursors don't work backwards, so I thought reversing the sort might be a good work around.

import arcpy
import itertools

fc = r"C:\Data\Workspace.gdb\MyFeatureClass"
sql_rev = (None, "ORDER BY X DESC")

inc = itertools.count(13,1)
expression = "{0} = 1 AND {1} = 1".format("Gpriority", "FlightID")
with arcpy.da.UpdateCursor(fc, ["FlightLnOrder", "X"], expression, sql_clause = sql_rev) as uCur:
    c = 1
    for row in uCur:
        c += 1
        if c == 2:
            row[0] = inc.next()
            c = 0
            uCur.updateRow(row)         

enter image description here

  • you need to indent the with block and then not try to del row or cursor.. the with block handles all that. The whole del line needs to be removed, none of those are objects that exist by the time the del statement is hit. – Michael Stimson Apr 25 '17 at 22:44
  • The indentation was an artifact of my adding the code to stack exchange. I am aware that the "with block" handles the deletion of objects, but it was useful to help quickly debug. The line "for row in uCur:" doesn't engage and values are not being written? – pyRsq Apr 25 '17 at 22:55
  • 1
    File geodatabase is NOT a database. – Vince Apr 25 '17 at 23:35
  • 1
    I just tried your suggested SQL here, but the clause requires 2 strings (prefix, postfix). X is a valid field name as shown in the image, it is a field of longitude values. I'm going to try playing with some of the other prefix options. If that doesn't work, create a feature layer with my ordered selection then try the update cursor on that. – pyRsq Apr 25 '17 at 23:37
  • 2
    Can you put this together as an answer to your own question, I think a working example of an update cursor with sql_clause would be invaluable. – Michael Stimson Apr 25 '17 at 23:45
3

To properly utilize the arcpy.da.UpdateCursor with an sql_clause you need to ensure your feature class is stored within a database, not a File Geodatabase or as a Shapefile in folder. In the provided solution a Microsoft (JET) Access personal geodatabase is utilized, however other databases can provide improved efficiencies with 64-bit support.

If your data is stored in a File Geodatabase the sql_clause may still function, but it can be unreliable or not work at all when attempting to process rows which already have values written to them.

The following link is provided to reference optional pairs of SQL prefix and postfix clauses organized in a list or tuple. http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000014000000

Below is the corrected code used to update the above table as shown. Reminder: the above table already had an update cursor run on it with an "ORDER BY X ASC" sql_clause.

import arcpy
import itertools

fc = r"C:\Data\New Personal Geodatabase.mdb\MyFeatureClass"
sql_rev = (None, "ORDER BY X DESC")

inc = itertools.count(13,1)
expression = "{0} = 1 AND {1} = 1".format("Gpriority", "FlightID")
with arcpy.da.UpdateCursor(fc, ["FlightLnOrder", "X"], expression, sql_clause = sql_rev) as uCur:
    c = 1
    for row in uCur:
        c += 1
        if c == 2:
            row[0] = inc.next()
            c = 0
            uCur.updateRow(row)      

enter image description here

Note for multiple arcpy.da.UpdateCursors with an sql_clause:

If you are attempting to use multiple arcpy.da.UpdateCursors, each with their own sql_clause, you need to wrap both cursors within an edit session and all data needs to be stored in a database.

http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000014000000 "Opening simultaneous insert or update operations on the same workspace using different cursors requires the start of an edit session." http://resources.arcgis.com/en/help/main/10.1/index.html#/Editor/018w00000005000000/

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.