0

I am trying to use arcpy.da.InsertCursor() to insert rows to an SDE table but no rows were added and without any error message. I tried the same code with an exact replica table in a local FGDB and it worked fine. The code is simple:

workspace = r"Database Connections\SDE.sde"
arcpy.env.workspace = workspace
arcpy.env.overwriteOutput = True

edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()

cursor = arcpy.da.InsertCursor(sde_table, ["Date_"])
cursor.insertRow(["10/21/2020"])

I also tried to use arcpy.InsertCursor() and it works well in both SDE and local FGDB. Is there anything missing for getting arcpy.da.InsertCursor() to work in SDE?

RDBMS: SQL Server 2012

Python: 2.7

SDE: 10.6.1

5
  • 1
    I haven't ever had a DA InsertCursor fail silently, so more details are necessary: Which RDBMS? What version? What version of ArcPy? What version of geodatabase? What are the exact column names? Have you tried using with so that the session is committed without an explicit del cursor?
    – Vince
    Oct 26 '20 at 16:57
  • @Vince, thanks for replying. I added the info in the question. I only have one column name right now bacause it's a test. I tried with but no luck.
    – PacmanKX
    Oct 26 '20 at 17:50
  • 2
    As presented, I wonder it that's a legal date (I always use ISO "YYYY-MM-DD HH:MM:SS"), but in a cursor you need to bind the right type (unless you're passing a string to a string field named "Date_"). The commit won't happen without the del cursor and edit directives.
    – Vince
    Oct 26 '20 at 18:24
  • OK, so I need to put in 2020-10-21 00:00:00, del cursor, and edit.stopOperation() and edit.stopEditing(True)?
    – PacmanKX
    Oct 26 '20 at 18:36
  • @Vince Thank you for your help. It's now working after adding del cursor, edit.stopOperation() and edit.stopEditing(True). What a silly mistake. Also, "10/20/2020" works fine. Seems no need to put in the ISO date. But I don't quite understand why the code used to work in local FGDB without these closing commands.
    – PacmanKX
    Oct 26 '20 at 19:15
2
import arcpy
import os

fc = r'Database Connections\Connection to sde.sde\table'
workspace = os.path.dirname(fc)


edit = arcpy.da.Editor(workspace)


edit.startEditing(False, True)


edit.startOperation()


with arcpy.da.InsertCursor(fc, ('field1', 'field2')) as cursor:
    cursor.insertRow(("string1", "string2"))



edit.stopOperation()


edit.stopEditing(True)

The above code should work for you. just replace the SDE location, field1 and field2 and finally the string1 and string2

4
  • Thank you for replying. I tried your suggestion but no luck. I also directly pasted the full SDE and table paths to it and still not working.
    – PacmanKX
    Oct 26 '20 at 17:51
  • Thank you. I added the missing edit.stopOperation() and edit.stopEditing(True) and it's working.
    – PacmanKX
    Oct 26 '20 at 19:16
  • I use the above code it worked for me. Happy to hear that it also worked for you.
    – Ram
    Oct 26 '20 at 19:27
  • Yes thanks for reminding me of the stop operation and stop editing - that's where the problems are. Nothing to do with the path or fields.
    – PacmanKX
    Oct 26 '20 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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