Cannot Acces SDE Connection File Properly using Arcpy

Discussion created by BrokenLegMike on Mar 8, 2014
Latest reply on Mar 12, 2014 by mzcoyle
I am testing a way around accessing sde connections using arcpy. I have 2 existing sde databases connected in ArcCatalogue that require passwords anytime I go to connect to them. It seems that there is no way to access existing database connections using arcpy (that I can find anyways). You are forced to create new ones every time if you want to do this in a script using:

arcpy.CreateArcSDEConnectionFile_management (folderName, fileName, serverName, serviceName, databaseName, authType, username, password)

This seems completely redundant to do so if I already have the connection set up. There should be a function to access these existing connection so that a programmer can just build a tool that allows the user to enter their user name and password without re-creating the connection.

Oh but what. You sort of can if you set your environment workspace in your script:

import arcpy
from arcpy import env

env.workspace = r"Database Connections\blah.sde"

When you do this and run the script from the ArcGIS GUI, the regular database connection prompt will pop up which should allow you to login. The problem I have having is, the prompt will come up but anytime I click on the window, the ArcGIS GUI will override it an won't allow me to type anything into the username and password fields.

So, I tried to get creative and decided to create a new connection file and then immediately after, I tried to delete it.

import arcpy

# Set variables
folderName = r"Database Connections"
fileName = "Connection to SDE.sde"
serverName = "someserver"
serviceName = "5151"
databaseName = ""
authType = ""
username = "username"
password = "password"

arcpy.CreateArcSDEConnectionFile_management (folderName, fileName, serverName, serviceName, databaseName, authType, username, password)

arcpy.Delete_management(r"Database Connections\Connection to SDE.sde")

The thinking was that I would just build the tool to allow the user to enter their username and password, pass that into the arcpy.CreateArcSDEConnectionFile function, do some processing and then delete the connection file at the end using

The problem is, it schema locks every time the script does the delete. This is weird because I simply created and deleted in one foul swoop.

I'm at a loss. I need to figure out a way to access a database connection. I need to either:

  1. Solve the issue with the ArcGIS GUI overriding the database connections login prompt

  2. Find a way to release the schema lock so that I can delete any database connections I create during the script

  3. Or find some other weird and wonderful to accomplish this