2

I have got follow code:

for row in arcpy.da.SearchCursor(db_from, fields, where_clause="objectid < 11111"):
    value_from = list(row)
    for rowto in arcpy.da.SearchCursor(db_to, fields, where_clause="uid = '" + str(row[0]) + "'"):
        value_to = list(rowto)
        if(set(value_from) == set(value_to)): # data is same
            same +=1
            print "data is same"
        else:
            with arcpy.da.UpdateCursor(db_to, fields, where_clause="uid = '" + str(row[0]) + "'") as cursor:
                for x in cursor:
                    cursor.updateRow(value_from)
                    updated +=1
                    print "data update"
    else:
        to_cursor.insertRow(value_from)
        print "Inserted"

print "Same Data: ", str(same)
print "Updated Data: ", str(updated)

I am getting next error:

data update
data update
data update
data update
data update
Inserted
Traceback (most recent call last):
  File "C:\code\Python\work\dbsync.py", line 69, in <module>
    for x in cursor:
RuntimeError: workspace already in transaction mode

Where I should to close transaction? Am I right understand that issue is rise when I do insert when transaction (cursor?) of update is still active?

I am using ArсPy

1

After updated +=1 use del row and del cursor

for row in arcpy.da.SearchCursor(db_from, fields, where_clause="objectid < 11111"):
    value_from = list(row)
    for rowto in arcpy.da.SearchCursor(db_to, fields, where_clause="uid = '" + str(row[0]) + "'"):
        value_to = list(rowto)
        if(set(value_from) == set(value_to)): # data is same
            same +=1
            print "data is same"
        else:
            with arcpy.da.UpdateCursor(db_to, fields, where_clause="uid = '" + str(row[0]) + "'") as cursor:
                for x in cursor:
                    cursor.updateRow(value_from)
                    updated +=1
            del x
            del cursor


                    print "data update"
    else:
        to_cursor.insertRow(value_from)
        print "Inserted"

print "Same Data: ", str(same)
print "Updated Data: ", str(updated)
  • wetland, could you explain why? Should I do delete cursor after every insert operation? – Dmitry Bubnenkov Nov 25 '16 at 13:09
  • After any update you should delete row and cursor to end the transaction and unlock the feature class – BBG_GIS Nov 25 '16 at 13:10
  • do not helped :( I also tried variant with del x that tabbed to right (it's look mistake in your code) – Dmitry Bubnenkov Nov 25 '16 at 13:15
  • I edited my code. I had a syntax error – BBG_GIS Nov 25 '16 at 13:16
  • wetland, do you sure that del x in your code is on right level? Do I need remove insert cursor? See that insert inside in global for loop. – Dmitry Bubnenkov Nov 25 '16 at 13:23

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.