Update attribute values in enterprise geodatabase with Python

A minimal example for updating attribute values in an ESRI enterprise geodatabase via a Python script.

Both versions presented below assume that you have direct access to the database through an .sde connection file.

Non-versioned enterprise geodatabase

import arcpy
import os

sde = r"D:\user@DATABASE.sde"
fc = "SCHEMA.table_name"

edit = arcpy.da.Editor(sde)
edit.startEditing(with_undo=False,multiuser_mode=False)
edit.startOperation()

with arcpy.da.UpdateCursor(os.path.join(sde,fc),"*") as cursor:
	for row in cursor:
		row[cursor.fields.index("field_name")] = "Some new value"
		cursor.updateRow(row)
		
edit.stopOperation()
edit.stopEditing(True)
del edit

See documentation for additional information:

Note that this example uses the UpdateCursor method, but you might also use the Calculate Field tool if it’s more appropriate for your use case.

Branch versioned enterprise geodatabase

Requires a published FeatureService. Acquire the access token from the Generate Token endpoint.

import arcpy
import os
import requests

sde = r"D:\user@DATABASE.sde"
fc = "SCHEMA.table_name"
apply_edits = r"https://example.com/server/rest/services/ExampleServerFolderName/ExampleServiceName/FeatureServer/0/applyEdits"
token = "JTEf45Sf6A7aa43dDhSSFGgesd4tyfDF5y574wf6A7aa43dGsd4tyff45Su."

updates = []

with arcpy.da.SearchCursor(os.path.join(sde,fc), "*") as cursor:
	for row in cursor:
		attributes = dict()
		attributes["GlobalID"] = row[cursor.fields.index("GlobalID")]
		attributes["field_name"] = "Some new value"
		updates.append({"attributes":attributes})

data = {
	"updates": str(updates),
	"gdbVersion": "SDE.DEFAULT",
	"f": "pjson",
	"token": token,
	"useGlobalIds": "true"
}

response = requests.post(url=apply_edits,
						 data=data,
						 headers={"Content-type":"application/x-www-form-urlencoded"})

See documentation for additional information:

Note that you could also query the FeatureService instead of using SearchCursor. Pick whichever method works better for the given use case.