[B]Update subtype field with arcpy.da.updateCursor()[/B]

718
3
Jump to solution
10-22-2013 02:33 AM
ben_abdallahmhd
New Contributor III
hello
i try to update a subtype field (integer) from the value of an other field with arcpy.da.updateCursor() but dosen't work.

please some idea

axeceau = "RHAXECEL" # the feature class fields = ['CODHYDRO', 'TYPECEAU'] # TYPECEAU is the subtype field with arcpy.da.UpdateCursor(axeceau, fields) as cursor8:     for row in cursor8:         if row[0] == 3:             row[1] = 4         elif row[0] == 4:             row[1] = 1         elif row[0] == 5:             row[1] = 1         else:             row[1] = 1         cursor8.updateRow(row) 
Tags (2)
1 Solution

Accepted Solutions
ben_abdallahmhd
New Contributor III
hello,
i find the mistake when i have this error message:
"Objects in this class cannot be updated outside an edit session [TOP10_SIG.DBO.RHAXECEL]".
So i edit the session with arcpy.da.Editor and the script run successfully.
There is th good script:
try:
    axeceau = "RHAXECEL"
    fields = ['CODHYDRO', 'TYPECEAU']
    edit = arcpy.da.Editor(arcpy.env.workspace)
    with arcpy.da.Editor(arcpy.env.workspace) as edit:
        cursor8 = arcpy.da.UpdateCursor(axeceau, fields)
#rows = arcpy.UpdateCursor(axeceau, fields)
        for row in cursor8:
            if row[0] == 3:
                row[1] = 4
            elif row[0] == 4:
                row[1] = 1
            elif row[0] == 5:
                row[1] = 1
            else:
                row[1] = 1
            cursor8.updateRow(row)
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

View solution in original post

3 Replies
NeilAyres
MVP Alum
What doesn't work? What error do you get.
Have you already defined what subtypes 1 & 4 are?
Cheers,
Neil
0 Kudos
ben_abdallahmhd
New Contributor III
What doesn't work? What error do you get.
Have you already defined what subtypes 1 & 4 are?
Cheers,
Neil


Thank you Neil Ayres,

doesn't work = it mean there is no result, the script does nothing 🙂
What error do you get? : No i don't get an error.
Have you already defined what subtypes 1 & 4 are? Yes,Subtypes are defined in the gdb.

the subtypes are :
1 Cours d'eau naturel
2 Canal d'alimentation ou d'irrigation
3 Canal de drainage ou de collecte des eaux pluviales
4 Fossé d�??assèchement ou de drainage ou de collecte des eaux pluviales

I use try and except to get error message but there is no message.
the script with try is this:
try:
    axeceau = "TOP10_SIG.DBO.RHAXECEL"
    fields = ['CODHYDRO', 'TYPECEAU']
    with arcpy.da.UpdateCursor(axeceau, fields) as cursor8:
#rows = arcpy.UpdateCursor(axeceau, fields)
        for row in cursor8:
            if row[0] == 3:
                row[1] = 4
            elif row[0] == 4:
                row[1] = 1
            elif row[0] == 5:
                row[1] = 1
            else:
                row[1] = 1
            cursor8.updateRow(row)
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

Thank you
0 Kudos
ben_abdallahmhd
New Contributor III
hello,
i find the mistake when i have this error message:
"Objects in this class cannot be updated outside an edit session [TOP10_SIG.DBO.RHAXECEL]".
So i edit the session with arcpy.da.Editor and the script run successfully.
There is th good script:
try:
    axeceau = "RHAXECEL"
    fields = ['CODHYDRO', 'TYPECEAU']
    edit = arcpy.da.Editor(arcpy.env.workspace)
    with arcpy.da.Editor(arcpy.env.workspace) as edit:
        cursor8 = arcpy.da.UpdateCursor(axeceau, fields)
#rows = arcpy.UpdateCursor(axeceau, fields)
        for row in cursor8:
            if row[0] == 3:
                row[1] = 4
            elif row[0] == 4:
                row[1] = 1
            elif row[0] == 5:
                row[1] = 1
            else:
                row[1] = 1
            cursor8.updateRow(row)
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))