Select to view content in your preferred language

updateConnectionProperties not working correct

72
1
yesterday
shay-geo
Frequent Contributor

Hello,


Following this guide, https://pro.arcgis.com/en/pro-app/3.3/arcpy/mapping/updatingandfixingdatasources.htm


Trying to update the connection properties of multiple files. The new way works for the majority of the datasets (including FCs, Raster, FC with joins in them) except when it has tables. This works fine from one gdb to another gdb but it breaks for TABLES when gdb to sde.
       

        aprx = arcpy.mp.ArcGISProject(r'some_aprx_path')

        aprx.updateConnectionProperties(

                    local_gdb_path, # local gdb file where data currently reside

                    'sde_connection_file.sde', # SDE connection file
                    auto_update_joins_and_relates=True,
                    validate=True,
                    ignore_case=True
                )

When I go to table and give a proper connection info (user, pwd, host etc) like below, it works.

  old = table.connectionProperties.copy()
  new = {
      "authentication_mode": "dbms",
      "database": 'db', 
     "dbclient": "sql", 
     "db_connection_properties": host, # The server
     "instance": "instance with sde",
     "server": host,
    "user": 'usr',
    "version": "", 
    "password": '****pwd',
   }
  table.updateConnectionProperties(old, new)


I was hoping all the dataset works the same way and not having to pass the connection info, instead just SDE ConnectionFile. It makes unit tests works better, since updateConnectionProperties from one gdb to another gdb works just file. Now I have to have a different path for the integration with connection string.

Thanks.
0 Kudos
1 Reply
TonyAlmeida
MVP Regular Contributor

Tables require explicit connection properties rather than just the connection file path and tables don't connection information the same way as feature layers do. 

 

untested

# Configuration
old_gdb_path = r"C:\Temp\database.gdb"
sde_connection_file = r"C:\Temp\connection.sde"

# Hardcoded SDE connection properties
sde_props = {
    "authentication_mode": "dbms",
    "database": "your_database_name",  # e.g. "sde" for SQL Server
    "dbclient": "sql",  # or "oracle", "postgresql" depending on your DBMS
    "db_connection_properties": "your_server_name",
    "instance": "sde:sqlserver:your_instance",  # adjust for your DBMS
    "server": "your_server_name",
    "user": "your_username",
    "version": "SDE.DEFAULT",
    "password": "your_password"  # Consider secure storage methods
}

# Load project
aprx = arcpy.mp.ArcGISProject("CURRENT")  # or specify path to .aprx

# Process all items
for m in aprx.listMaps():
    print(f"Processing map: {m.name}")
    
    # Process layers
    for lyr in m.listLayers():
        if lyr.supports("DATASOURCE"):
            old_conn = lyr.connectionProperties
            if old_conn.get('database', '').lower() == old_gdb_path.lower():
                print(f"  Updating layer: {lyr.name}")
                lyr.updateConnectionProperties(old_conn, {"database": sde_connection_file})
    
    # Process tables 
    for table in m.listTables():
        old_conn = table.connectionProperties
        if old_conn.get('database', '').lower() == old_gdb_path.lower():
            print(f"  Updating table: {table.name}")
            table.updateConnectionProperties(old_conn, sde_props)

 

0 Kudos