arcpy.da.InsertCursor returning 'RuntimeError' error message

773
2
Jump to solution
12-01-2020 05:42 PM
AnthonyCheesman1
Occasional Contributor II

Gday everyone

I've hit a strange problem with a Python process I'm trying to build that uses a couple of arcpy.da cursors.

The way the process is structured, firstly I am using a arcpy.da.InsertCursor to add rows to a feature class based on logic from a prior step. The process is running fine up until this point.

I then need to call an arcpy.da.UpdateCursor on the same feature class, and this is where I'm striking the "Runtime error - cannot open file feature_class_name".

I thought maybe it was due to the cursor from the InsertCursor object still being live, so I've refactored the script to explicitly delete the object, with no luck. I also thought there may be a race condition where the UpdateCursor was being called before the InsertCursor object was deleted, so I explicitly built in a 5 second pause. No success either.

I'm 100% certain I have the syntax right (but have double and triple checked) - and I'm at a bit of a loss with this issue.

Has anyone got any suggestions that may send me in the right direction?

On ArcMap 10.7.1 and code below.

 

hashes_source = []
with arcpy.da.SearchCursor(source_ch_data,fieldlist[0:-4]) as cur:
    for row in cur:
        sourcehash = get_hash(row)
        hashes_source.append(sourcehash)
        if sourcehash not in hashes:
            print "Inserting row for",sourcehash
            icur = arcpy.da.InsertCursor("PRELIMINARY_REPORTS_ALL",fieldlist)
            icur.insertRow(row+("Y",last_update_dto,None,sourcehash))
            del icur

time.sleep(5)

# process runs ok to this point, fails where UpdateCursor is called below
# error message below:

'''
Traceback (most recent call last):
  File "C:\folder_location\change_detection.py", line 101, in <module>
    with arcpy.da.UpdateCursor("PRELIMINARY_RECORDS_ALL",["CURRENT_RECORD_YN","RECORD_RETIRED_DT","HASH_SHA256_HEX"]) as cur:
RuntimeError: cannot open 'PRELIMINARY_RECORDS_ALL'
'''

# iterate through destination table
# if hash not in source table, retire the record
with arcpy.da.UpdateCursor("PRELIMINARY_RECORDS_ALL",["CURRENT_RECORD_YN","RECORD_RETIRED_DT","HASH_SHA256_HEX"]) as cur:
    for row in cur:
        if row[2] not in hashes_source:
            print "Retiring record",row[2]
            row[0] = "N"
            row[1] = last_update_dto
            cur.updateRow(row)

 

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
AnthonyCheesman1
Occasional Contributor II

Issue found, and it's **bleep** embarrassing I must admit - I stuffed up the name of the feature class. And despite double, triple and quadruple checking - it still slipped through the cracks.

The correct name was PRELIMINARY_REPORTS_ALL, but I was trying to open a cursor on PRELIMINARY_RECORDS_ALL (which doesn't exist).

Problem solved...

View solution in original post

2 Replies
wwnde
by
Occasional Contributor

Could you please provide the tables you are trying to edit/update.

 

Spatial enable dataframes  could achieve the same and I have found them much easier and a way of the future I think

0 Kudos
AnthonyCheesman1
Occasional Contributor II

Issue found, and it's **bleep** embarrassing I must admit - I stuffed up the name of the feature class. And despite double, triple and quadruple checking - it still slipped through the cracks.

The correct name was PRELIMINARY_REPORTS_ALL, but I was trying to open a cursor on PRELIMINARY_RECORDS_ALL (which doesn't exist).

Problem solved...