Traceback (most recent call last): File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 45, in onMouseDownMap cursor.insertRow(row) RuntimeError: Objects in this class cannot be updated outside an edit session [TonyTwoWay.DBO.TT]import arcpy import pythonaddins import os from arcpy import env class Add_points(object): """Implementation for AddPoints_addin.Add_points (Tool)""" def __init__(self): self.enabled = True self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks. def onMouseDownMap(self, x, y, button, shift): fc = "TonyTwoWay.DBO.TT" workspace = r"Database Servers\DSD15_SQLEXPRESS.sde" arcpy.env.overwriteOutput = True # Start an edit session. Must provide the worksapce. edit = arcpy.da.Editor(env.workspace) # Edit session is started without an undo/redo stack for versioned data # (for second argument, use False for unversioned data) edit.startEditing(True) # Start an edit operation edit.startOperation() list = [] with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor: for row in cursor: try: if "CC" in row[0]: list.append(int(row[0].strip("CC"))) except TypeError: pass del cursor list.sort() AddressID = list[-1] + 1 AddressID = 'CC' + str(AddressID) row_values = [(x, y, (x, y), AddressID)] cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"]) for row in row_values: cursor.insertRow(row) del cursor arcpy.RefreshActiveView() # Stop the edit operation. edit.stopOperation() # Stop the edit session and save the changes edit.stopEditing(True) pass
Solved! Go to Solution.
row_values = [(x, y, (x, y), AddressID)]
cursor = arcpy.da.InsertCursor(fc)
for x in row_values:
row = cursor.newRow()
row.setValue("YourField", x(0)) # Fix to match your list values
cursor.insertRow(row)
del cursor
row_values = [(x, y, (x, y), AddressID)] cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"]) field_names = ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"] for x in row_values: row = cursor.newRow() row.setValue(field_names, x(0)) # Fix to match your list values cursor.insertRow(row) del cursor
>>>
Traceback (most recent call last):
File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 47, in onMouseDownMap
row = cursor.newRow()
AttributeError: 'da.InsertCursor' object has no attribute 'newRow'
>>>
See the InsertCursor help for how to use an insertcursor. You cannot specify a field list with an insertcursor.
I am trying to run a script as a tool but i get an error on cursor.insertRow(row) and i don't know why.
Could someone help me out please?
Error.Traceback (most recent call last): File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 45, in onMouseDownMap cursor.insertRow(row) RuntimeError: Objects in this class cannot be updated outside an edit session [TonyTwoWay.DBO.TT]
code i am trying to runimport arcpy import pythonaddins import os from arcpy import env class Add_points(object): """Implementation for AddPoints_addin.Add_points (Tool)""" def __init__(self): self.enabled = True self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks. def onMouseDownMap(self, x, y, button, shift): fc = "TonyTwoWay.DBO.TT" workspace = r"Database Servers\DSD15_SQLEXPRESS.sde" arcpy.env.overwriteOutput = True # Start an edit session. Must provide the worksapce. edit = arcpy.da.Editor(env.workspace) # Edit session is started without an undo/redo stack for versioned data # (for second argument, use False for unversioned data) edit.startEditing(True) # Start an edit operation edit.startOperation() list = [] with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor: for row in cursor: try: if "CC" in row[0]: list.append(int(row[0].strip("CC"))) except TypeError: pass del cursor list.sort() AddressID = list[-1] + 1 AddressID = 'CC' + str(AddressID) row_values = [(x, y, (x, y), AddressID)] cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"]) for row in row_values: cursor.insertRow(row) del cursor arcpy.RefreshActiveView() # Stop the edit operation. edit.stopOperation() # Stop the edit session and save the changes edit.stopEditing(True) pass
Traceback (most recent call last):
File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 18, in onMouseDownMap
edit = arcpy.da.Editor(arcpy.env.workspace)
RuntimeError: cannot open workspaceworkspace = r"Database Servers\DSD15_SQLEXPRESS.sde" edit = arcpy.da.Editor(arcpy.env.workspace)
Traceback (most recent call last):
File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 45, in onMouseDownMap
cursor.insertRow(row)
RuntimeError: Objects in this class cannot be updated outside an edit session [TonyTwoWay.DBO.TT]
import arcpy
import pythonaddins
import os
from arcpy import env
class Add_points(object):
"""Implementation for AddPoints_addin.Add_points (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onMouseDownMap(self, x, y, button, shift):
fc = "TonyTwoWay.DBO.TT"
arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.sde"
arcpy.env.overwriteOutput = True
arcpy.ChangeVersion_management('TonyTwoWay.DBO.TT','TRANSACTIONAL','dbo.DEFAULT', "")
# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(arcpy.env.workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
list = []
with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:
for row in cursor:
try:
if "CC" in row[0]:
list.append(int(row[0].strip("CC")))
except TypeError:
pass
del cursor
list.sort()
AddressID = list[-1] + 1
AddressID = 'CC' + str(AddressID)
row_values = [(x, y, (x, y), AddressID)]
cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])
for row in row_values:
cursor.insertRow(row)
del cursor
del cursor
arcpy.RefreshActiveView()
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
pass
fc = "TonyTwoWay.DBO.TT" workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde" # Start an edit session. Must provide the worksapce. edit = arcpy.da.Editor(workspace)