[GIS] ArcPy Data Access Insert Cursor not writing geometries to feature class

arcgis-10.2arcpycursorgeometrypyscripter

I'm using PyScripter and running ArcGIS 10.2.1.

I am having troubles with the Insert Cursor in the arcpy module.

I have an empty feature class in my geodatabase and have created the short script below to try and find the issue, but have not been able to. When I run the script, it executes fine, but when I look in the attribute table of the feature class afterwards it is still empty.

import arcpy

fc = r"C:\Users\djh\Desktop\topo_map\test.gdb\well_location"
cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
xy = (206901.75, 5997594.47)

cursor.insertRow([xy])

I have copied this script straight from the "writing geometries" page on the ArcGIS help website, so I don't think it's a problem with syntax. Please let me know if there's a way to get around this issue.

Best Answer

PyScripter is somewhat lax with object lifetimes and will keep stuff around after it's run. Use the with statement to ensure you close the edit session.

import arcpy

fc = r"C:\Users\djh\Desktop\topo_map\test.gdb\well_location"
xy = (206901.75, 5997594.47)

with arcpy.da.InsertCursor(fc, ["SHAPE@XY"]) as cursor:
    cursor.insertRow([xy])
Related Question