Setting SDE connection as workspace

15451
8
10-24-2012 09:50 AM
MikeBly
Occasional Contributor
Hi,

I am trying to run a simple script that simply copies feature datasets from one file geodatabase to another. This is successful, however now I am attempting to copy from a versioned SDE geodatabase to a file geodatabase. I have set the workspace to the SDE using the following:

# Set environment settings
arcpy.env.workspace = r"C:\Users\mbly\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\COL_LCT_Base.sde"

When I run the script I get return code '0', assuming no errors, however nothing happens, the script does not run because i get the code within a fraction of a second. I am confused as to how to get the script to look at the SDE.

Do I have to create a script to connect as a direct connection with all the login credentials inside the script. I am sure this has been done many times over, this is nothing new.

Any ideas, direction?

Cheers,

Mike
Tags (2)
0 Kudos
8 Replies
T__WayneWhitley
Frequent Contributor
I think if you have the appropriate connection file (or can make one), you can save the version to the connection, correct?

Then I think you can just use this to set the workspace if I'm not missing something:
arcpy.env.workspace = r"Database Connections\COL_LCT_Base.sde"
0 Kudos
MikeBly
Occasional Contributor
I have a created a connection file and I am pretty sure it is correct, do have to call this file into the script or make sure I store it somewhere specific?

Cheers,

Mike
0 Kudos
T__WayneWhitley
Frequent Contributor
I'm not sure what you mean?  Are you saying you are not able to connect or do you want to know how to test the connection?

If you just want to see if you're able to read something, say a fc named ownedByMe.MyFeatureClass at your connection, COL_LCT_Base.sde, you can check for existence:

import arcpy
arcpy.env.workspace = "Database Connections/COL_LCT_Base.sde"

fc = "ownedByMe.MyFeatureClass"

if arcpy.Exists(fc): 
    print "Check succeeded - all guests are here, so proceed with the party!"


The 'Database Connections' is simply a shortcut ArcGIS (arcpy) understands as a default location for connection files --- however, you do not have to store your connection files there and you can even create the connection 'from scratch' in the script.  If you just want to create a connection file and maybe move it to a more 'inconspicuous' location, you can do that and just refer to it with a full pathname.

Also, if you'd rather do something like list data available from your defined connection, there are a set of commands for that, summarized here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z00000011000000
SendhilKolandaivel
New Contributor
Mike,

You may have to loop through the Feature Datasets and copy them one at a time, as shown in the code example below:

import arcpy
import os

arcpy.env.overwriteOutput = True

outFGDB = r"YOUR_FGDB.gdb"
sdeWorks = r"Database Connections\YOUR_DB_CONNECTION.sde"

arcpy.env.workspace = sdeWorks

try:
    for fd in arcpy.ListDatasets("*"):
        print "Processing", fd, "..."
        arcpy.env.workspace = sdeWorks + os.sep + fd
        outData = outFGDB + os.sep + fd.split('.')[1]
        arcpy.Copy_management(arcpy.env.workspace, outData, "FeatureDataset")
except:
    arcpy.GetMessages()


Best Regards,

Sendhil
0 Kudos
RonAnkeny
New Contributor III
I'm having a similar problem, but I cannot see what I am doing wrong.


import arcpy
from arcpy import env

env.workspace = r"F:Replicated Data\Auto_Extract_Connection.sde"


datasetList = arcpy.ListDatasets()

print datasetList

env.workspace =  r"F:\Replicated Data\Gatekeeper_Extract.gdb"

datasetList = arcpy.ListDatasets()

print datasetList


This returns

[]
[u'ElectricDataset', u'CPUDataset', u'TransmissionDataset']

It's clearly connecting to the file GDB but won't connect to the SDE one. I know the connection file is working, I can connect with it through Catalog.
0 Kudos
benberman
Occasional Contributor
I'm having a similar problem, but I cannot see what I am doing wrong.


import arcpy
from arcpy import env

env.workspace = r"F:Replicated Data\Auto_Extract_Connection.sde"


datasetList = arcpy.ListDatasets()

print datasetList

env.workspace =  r"F:\Replicated Data\Gatekeeper_Extract.gdb"

datasetList = arcpy.ListDatasets()

print datasetList


This returns

[]
[u'ElectricDataset', u'CPUDataset', u'TransmissionDataset']

It's clearly connecting to the file GDB but won't connect to the SDE one. I know the connection file is working, I can connect with it through Catalog.


try this:

arcpy.env.workspace = r'F:Replicated Data\Auto_Extract_Connection.sde'
... for ds in arcpy.ListDatasets():
...     print ds
0 Kudos
T__WayneWhitley
Frequent Contributor
Typo - missing '\' in 1st pathname... I think F:Replicated Data should be F:\Replicated Data, provided you have both the given sde connection file and the gdb at that root directory.

You've probably seen that by now, and this is a case where using 'join' from 'os.path' would be useful in avoiding such an error, as in something like this:
import arcpy, os
from arcpy import env

root = r"F:\Replicated Data"

for workspace in ["Auto_Extract_Connection.sde", "Gatekeeper_Extract.gdb"]:
     env.workspace = os.path.join(root, workspace)
     datasetList = arcpy.ListDatasets()
     print workspace, datasetList


...or, could even use ListWorkspaces, I think (I didn't test this)-
import arcpy, os
from arcpy import env

root = env.workspace = r"F:\Replicated Data"

workspaces = arcpy.ListWorkspaces()

for workspace in workspaces:
     env.workspace = workspace
     datasetList = arcpy.ListDatasets()
     print workspace, datasetList
0 Kudos
RonAnkeny
New Contributor III
Thank you Wayne. That missing \ was the ticket. I just could not see it, no matter how long I stared at the code.
0 Kudos