Get list of database connections in arcpy

1844
5
10-29-2021 12:02 AM
Vakhtang_Zubiashvili
Occasional Contributor III

Hi guys,

I want to get list of my database connections (see pic.)  in Arcpy to display them in my tool dropdown list, i try next code:

workspaces = arcpy.ListWorkspaces("*", "SDE")
    for workspace in workspaces:
        print workspace

but it does not give any result.  How can i do that?

Thank you

Capture.PNG

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

ListWorkspaces—ArcGIS Pro | Documentation

what did the print statement produce?

where in your code did you set your workspace to start the search?

The workspace environment must be set before using several of the list functions, including ListDatasets, ListFeatureClasses, ListFiles, ListRasters, ListTables, and ListWorkspaces.


... sort of retired...
0 Kudos
Vakhtang_Zubiashvili
Occasional Contributor III

Hi Dan,

1) Question: what did the print statement produce?

Answer: Print statement should produce list of database connections i have in TOC. (See pic. please)

Capture.PNG

2) Question: where in your code did you set your workspace to start the search?

Answer: I set workspace at the beginning of my code, after Import arcpy and import os.

 

I might on wrong way and try to achieve wrong result, but i thought this is a right way :(.

0 Kudos
DanPatterson
MVP Esteemed Contributor

You will need to post the code that show that and the actual workspace so one can assess whether your database connections are associated with it


... sort of retired...
0 Kudos
DominicRoberge2
Occasional Contributor III

Hello, for me all me SDE connections are in the same folder (workspace) so if you define the workspace, you should get your listing of SDE databases. Worked for me. 🙂 (in Catalog, right click the SDE connection, select properties and look at the path of the "name")

import arcpy

arcpy.env.workspace = r"C:\Users\USERNAME\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog"

# List all file geodatabases in the current workspace
workspaces = arcpy.ListWorkspaces("*", "SDE")

for workspace in workspaces:
    print workspace

 

 

0 Kudos
WilliamChandler
New Contributor

If I was looking for a list of SDE connection in the workspace I would do it this way

import arcpy, os
ROOT_DIR = os.path.dirname(arcpy.env.workspace)
for file in os.listdir(ROOT_DIR):
if file.endswith(".SDE"):
print(file)

0 Kudos