Skip To Content

InsertCursor

Summary

Inserts rows into a feature class, shapefile, or table. InsertCursor returns an enumeration object that hands out Row objects.

Discussion

Legacy:

The arcpy.da cursors (arcpy.da.SearchCursor, arcpy.da.UpdateCursor, and arcpy.da.InsertCursor) were introduced with ArcGIS 10.1 to provide significantly faster performance over the previously existing set of cursor functions (arcpy.SearchCursor, arcpy.UpdateCursor, and arcpy.InsertCursor). The original cursors are provided only for continuing backward compatibility.

New Row objects can be obtained using the newRow method on the enumeration object into which rows are to be inserted. Each call to insertRow on the cursor creates a new row in the table whose initial values are set to the values in the input row.

Syntax

InsertCursor (dataset, {spatial_reference})
ParameterExplanationData Type
dataset

The table, feature class, or shapefile into which rows will be inserted.

String
spatial_reference

Coordinates are specified in the spatial_reference provided and converted on the fly to the coordinate system of the dataset.

SpatialReference
Return Value
Data TypeExplanation
Cursor

Returns a Cursor object against the specified feature class, shapefile, or table.

Code sample

InsertCursor example

Inserts 25 new rows into a table.

import arcpy

# Create insert cursor for table
rows = arcpy.InsertCursor("c:/base/data.gdb/roads_lut")

# Create 25 new rows. Set the initial row ID and distance values
for x in range(1, 26):
    row = rows.newRow()
    row.setValue("rowid", x)
    row.setValue("distance", 100)
    rows.insertRow(row)

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

Related topics