Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Insert Cursor fails to write results to table. The Search Cursor is returning the results, but errors when trying to insert into field.

# Insert Cursor for GDB table to GDB table  
# Import modules, environment settings
import arcpy, traceback, string
arcpy.env.overwriteOutput = True

# Database connection variables on SPGISPROC1
OutFGDB = r"P:\batch\APO\District_overlay.gdb\pa_fp_dat1"
InFGDB = r"P:\batch\APO\District_overlay.gdb\pa_fp_rel1"

#Create cursor for input table items
InCursor = arcpy.SearchCursor(InFGDB)

# Create a variable that stores the value for column in a given row of the input table

for InRow in InCursor:

    parcel      = InRow.getValue('pafp_frq_PARCEL')  
    fp_zone1    = InRow.getValue('pafp_frq_ZONE2') 

    # Copy the stored value from the input table into the output table
    gdbCursor = arcpy.InsertCursor(OutFGDB)
    row = gdbCursor.newRow()

    if fp_zone1:
        fp_zone1 = fp_zone1.upper

    #Fill out fields with variables from input table        
    row.pafp_frq_PARCEL = parcel
    row.pafp_frq_ZONE2  = fp_zone1    

    #Insert new row into output file 
    gdbCursor.insertRow(row)

#Delete cursor and row objects to remove locks on the data 
del row
del cursor

Error Message:

Traceback (most recent call last):
  File "C:\Python27\ArcGISx6410.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\gisdata\Python\Insert_table_Cursor.py", line 28, in <module>
    row.pafp_frq_PARCEL = parcel
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
RuntimeError: ERROR 999999: Error executing function.
share|improve this question
1  
Typically, this message comes up when you are trying to insert a value into a field that does not exist or if you are trying to insert a value that is the incorrect data type for the field. –  Brian Dec 13 '13 at 15:44
1  
You might also try moving the creation of the InsertCursor outside the for loop. Because you create the InsertCursor within the for loop, you are actually creating multiple InsertCursor's which may be creating locks on your dataset. –  Brian Dec 13 '13 at 15:46
add comment

1 Answer 1

up vote 4 down vote accepted

First thing I would do is to create your insert cursor outside of your for loop iterating on your search cursor; you are unnecessarily creating your insert cursor for each feature in InFGDB. And with respect to that insert cursor, you are not deleting it after each iteration which is likely why you are not seeing the new rows.

Second, and this is totally stylistic, but I would recommend using some better variable names to help keep things straight.

Here's how I'd write it:

# Insert Cursor for GDB table to GDB table  
# Import modules, environment settings
import arcpy, traceback, string
arcpy.env.overwriteOutput = True

# Database connection variables on SPGISPROC1
outFC = r"P:\batch\APO\District_overlay.gdb\pa_fp_dat1"
inFC = r"P:\batch\APO\District_overlay.gdb\pa_fp_rel1"

#Create cursor for input table items
sCur = arcpy.da.SearchCursor(inFC,['pafp_frq_PARCEL','pafp_frq_ZONE2']) # make sure the field names in the list correspond to your input featureclass
iCur = arcpy.da.InsertCursor(outFC,['pafp_frq_PARCEL','pafp_frq_ZONE2']) # make sure the field names in the list correspond to your output featureclass

for sRow in sCur:
    # I think the arcpy row object behaves differently than other tuples so we have to explicitly assign each value in the row to our new variables
    # parcel,fp_zone1 = sRow
    parcel = sRow[0]  
    fp_zone1 = sRow[1]
    if fp_zone1 is not None:
        try: fp_zone1 = fp_zone1.upper
        except: pass

    # Copy the stored value from the input table into the output table
    iCur.insertRow((parcel,fp_zone1))

del sCur,iCur

If this still fails, we can do some more testing to ensure the appropriate fields exist in your target featureclass.

share|improve this answer
    
Also would be a good idea to convert the cursors to da cursors and to restrict the cursor fields for both the search and insert cursors to just the two relevant fields. Performance will be dramatically better. –  blord-castillo Dec 13 '13 at 16:01
    
@blord-castillo yes, definitely would suggest that as well if he's running 10.1. –  Jason Dec 13 '13 at 16:10
1  
Yep, must be 10.1. He is using 10.1 according to the traceback. –  blord-castillo Dec 13 '13 at 17:21
    
Thanks for the suggestions. I have incorprated Jasons changes to the Code, However we are still getting the error code. What changes could be made to use the da cursors? –  Jack Avis Dec 13 '13 at 17:58
    
To go with @Brians suggestion, verify that the fields you are trying to populate in the new rows exist, are of the right type, and are spelled correctly. –  Jason Dec 13 '13 at 18:20
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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