3

I need to open a MXD file from a Python toolbox (so that I can automatically export it to a PDF). The file contains a connection to an Oracle 11g database, and I need to somehow provide the credentials to the database. None of the methods I have tried so far have worked - the PDF always comes out without any of the data from the database. How can I tell ArcGIS what username and password to use?

Attempt 1: Reference a SDE file:

arcpy.env.workspace = "credentials.sde"
mxd = arcpy.mapping.MapDocument(mxd_file)
arcpy.mapping.ExportToPDF(mxd, pdf_file)

Attempt 2: Create a SDE file:

arcpy.CreateArcSDEConnectionFile_management (
   out_folder_path = "C:\\Path",
   out_name = "temp.sde",
   server = "prod.world",
   service = "prod.world",
   account_authentication = "DATABASE_AUTH",
   username = "username",
   password = "password",
   save_username_password = "SAVE_USERNAME"
)
mxd = arcpy.mapping.MapDocument(mxd_file)
arcpy.mapping.ExportToPDF(mxd, pdf_file)

I am unsure of what I should use for the parameters server and service? When I look at the connection properties of an SDE file created from ArcCatalog it doesn't have those options. Instead it has the single option "Instance" with the value "sde:oracle11g:prod.world", and "Oracle" selected in the "Database platform" dropdown instead of "Application Server".

Attempt 3: Somehow make the MXD remember the username and password. Unsure of how to do that, though.

  • The SDE connection file needs to be set as the data source for the layer in the mxd. I would just loop through the layers one by one set the datasources like lyr.findAndReplaceWorkspacePath(find_workspace_path=lyr.dataSource, replace_workspace_path=r"C:\Newpath\To\SDE_ConnectionFile.sde") More info here: resources.arcgis.com/en/help/main/10.2/index.html#/… – Eok Oct 9 '15 at 16:24
  • 1
    Try setting service = "sde:oracle11g:prod.world" and server = " " (a single space, since it's a required value but not needed). That worked for my connection to an Oracle database with SDE. – Eok Oct 9 '15 at 16:38
3

If you use a direct connection, create a connection file to your database in Catalog window > Database Connections > Add DataBase Connection. In this case your Instance would be something like sde:oracle11g:servername

If you use an ArcSDE Service connection (not direct connection), create a connection with Create ArcSDE Connection File geoprocessing tool. In this case the Service parameter would be the ArcSDE Service name or port (e.g. 5151).

Either way check Save username and password.

Open your MXD with ArcMap. Remove any layer that is from your enterprise (oracle) database and re-add them from the connection file you have created in the previous step.

  • 1
    Thansk for the reply! This helped me on the right track. To get it to work, I had to do some modifications though - see my answer for details. – Anders Oct 12 '15 at 12:36
3

In the end I managed to solve this by using a combination of Farid Chers answer and Eok Ns comment.

First, I created a SDE file with the following code:

arcpy.CreateArcSDEConnectionFile_management (
   out_folder_path = "C:\\Path",
   out_name = "temp.sde",
   server = " ", #Note the single space!
   service = "sde:oracle11g:prod.world",
   account_authentication = "DATABASE_AUTH",
   username = "username",
   password = "password",
   save_username_password = "SAVE_USERNAME"
)

For some reason just creating it from ArcCatalog will not work, as only the username and not the password is saved.

Then I modified my MXD to use this connection. I did not have to remove the layer and add it again (as Farid suggested). It was enough to just go to "Properties" -> "Source" -> "Change query", and there change to the SDE i just created in the "Connection" drop down menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.