loop through SDE connections

1019
2
06-11-2014 10:39 AM
JeffGodfrey
New Contributor II
What want a python loop to go through a list of different SDE connections.  I have this:

SDEConnect = 'Database Connections\\Atlas2013' EditorList 'as' EditorList'.sde\\sde.CHELAN.LandBase\\sde.CHELAN.Property_Polygons'

That has an output of: ('Database Connections\\Atlas2013', 'cathye', 'as', 'cathye', '.sde\\sde.CHELAN.LandBase\\sde.CHELAN.Property_Polygons')

which doesn't work in the script.  How do I format the code to correctly insert the value from the EditorList?

Thanks
Jeff
Tags (2)
2 Replies
ShawnThorne
New Contributor III
Have you considered placing all of your SDE Connection Files in a List then Loop through the List.

Here's an example that returns the number of Feature Datasets and Standalone Feature Classes in each of the different SDE Connection Files contained within a List :


import arcpy

def main():
    try:
        sdeConnList = ["Database Connections\\ORCL - SDE@SERVER.sde",
                       "Database Connections\\ORCL1 - SDE@SERVER1.sde",
                       "Database Connections\\ORCL2 - SDE@SERVER2.sde",
                       "Database Connections\\ORCL3 - SDE@SERVER3.sde"]

        for sdeConn in sdeConnList:
            fcCount = 0
            fdCount = 0

            wk = arcpy.env.workspace = sdeConn

            if arcpy.Exists(wk):
                print("\n\n  SDE Connection File EXISTS :  " + sdeConn)

                fdList = arcpy.ListDatasets("*")
                for fd in fdList:
                    fdCount += 1


                fcList = arcpy.ListFeatureClasses("*")
                for fc in fcList:
                    fcCount += 1

                print("   # of Feature Datasets in this workspace : {0}".format(str(fdCount)))
                print("   # of Standalone Feature Classes in this workspace : {0}".format(str(fcCount)))

            else:
                print("\n\n SDE Connection File DOES NOT EXIST :  " + sdeConn)

        print("\n\n COMPLETED!! \n\n")

    except arcpy.ExecuteError:
        print (arcpy.GetMessages(2))

    except Exception as e:
        print (e[0])

if __name__ == '__main__':
    main()




Results :




SDE Connection File DOES NOT EXIST : Database Connections\ORCL - SDE@SERVER.sde


SDE Connection File EXISTS : Database Connections\ORCL1 - SDE@SERVER1.sde
   # of Feature Datasets in this workspace : 9
   # of Standalone Feature Classes in this workspace : 43


SDE Connection File EXISTS : Database Connections\ORCL2 - SDE@SERVER2.sde
   # of Feature Datasets in this workspace : 5
   # of Standalone Feature Classes in this workspace : 13


SDE Connection File EXISTS : Database Connections\ORCL3 - SDE@SERVER3.sde
   # of Feature Datasets in this workspace : 10
   # of Standalone Feature Classes in this workspace : 27


COMPLETED!!

ChristianWells
Esri Contributor

You could try something like this to format your connection string:

EditorList = ['user1', 'user2', 'user3']
SDEConnect = 'Database Connections\\Atlas2013 {0} as {0}.sde\\sde.CHELAN.LandBase\\sde.CHELAN.Property_Polygons'

#Print single user connection

print SDEConnect.format(EditorList[1])

#Print all users connection information

for editor in EditorList:

    print SDEConnect.format(editor)

Results:

Database Connections\Atlas2013 user1 as user1.sde\sde.CHELAN.LandBase\sde.CHELAN.Property_Polygons

Database Connections\Atlas2013 user2 as user2.sde\sde.CHELAN.LandBase\sde.CHELAN.Property_Polygons

Database Connections\Atlas2013 user3 as user3.sde\sde.CHELAN.LandBase\sde.CHELAN.Property_Polygons

Reply
0 Kudos