Select to view content in your preferred language

How to use UpdateCursor on a Versioned Feature Class

73
4
yesterday
JoshMakesMaps
New Contributor III

I'm working with a versioned feature class in ArcGIS Pro, which is connected to an enterprise SDE database. My goal is to iterate through all the records and update fields with only blank or whitespace values to Null.

To start, I'm using the script below to return the OBJECTID for records with whitespace values of the feature class within my active map frame. However, it seems like the script is referencing the non-versioned feature class and not returning the expected results from my test cases.

import arcpy

# Set the workspace and feature class
arcpy.env.workspace = r"file_path_to_connection_file.sde"
feature_class = "fc_A"

# Get the list of fields in the feature class
fields = arcpy.ListFields(feature_class)

# Start a search cursor to iterate through the records
with arcpy.da.SearchCursor(feature_class, ["OBJECTID"] + [field.name for field in fields if field.type == "String"]) as cursor:
    for row in cursor:
        object_id = row[0]
        for i, value in enumerate(row[1:], start=1):
            if value is not None and value.strip() == "":
                print(f"Record with OBJECTID {object_id} has a blank field: {fields[i].name}")

print("Script completed.")

Is there a specific way to ensure the script is referencing the versioned feature class? Any suggestions on why my test cases might not be returning as expected?

 

0 Kudos
4 Replies
AllenDailey1
Occasional Contributor III

Hi, Have you made sure that your connection file is pointing to the version?  

0 Kudos
JoshMakesMaps
New Contributor III

I'm not exactly sure how to point to a version of the connection.

0 Kudos
RhettZufelt
MVP Notable Contributor

You can configure what 'version' the SDE connection file is connected to.

In Catalog pane, right click the database and choose Geodatabase Connection Properties.  In that dialog box, you can select which version that particular connection file references.

RhettZufelt_0-1724172277588.png

 

R_

 

AlfredBaldenweck
MVP Regular Contributor

If coding from scratch, you can create an SDE file pointing directly to the version:

arcpy.management.CreateDatabaseConnection(inFolder,
                                                  "TempConnectionFile.sde", 
                                                  "SQL_SERVER", 
                                                  instance = "instance", 
                                                  "OPERATING_SYSTEM_AUTH",
                                                  # username = "",
                                                  # password = "",
                                                  database = "ilmwytest2", 
                                                  version_type = "TRANSACTIONAL", 
                                                  version = 'owner.fred_testing')

 

Then you can do your thing. Just beware you'll probably have to use an Editor object if you want to use an Update Cursor.