6

It seems like arcpy can only work with connection files. This creates one of two problems:

  1. Deployment requires manual creation of connection files.
  2. We have to write a bunch of code to recreate the connection file on each run. (Creating it only the first time is not feasible since the connection information may change.)

As a software developer who does not have access to the production environment, I want to minimize manual steps in the deployment, and I want to keep my code simple. I also want this information to be very easy to change and not require opening up ArcMap (which would be manual), so I want to store it in a simple text config file and establish connections on the fly in my script.

Is there a way to create a connection to a database without creating a connection file? An "in memory only" connection (like is possible with pretty much any other database technology, even Oracle, or a number of other libraries, like GDAL)?

I'm using ArcGIS 10.2, and I will only be establishing query layers.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
jpmc26
  • 1,709
  • 1
  • 13
  • 30
  • As @Luke noted in comment to an answer, it's possible from Arcobjects. He has an example here: https://code.google.com/p/metageta/source/browse/tools/arcobjectscom.py?r=772 – matt wilkie Jan 08 '15 at 17:55

1 Answers1

6

You could create temporary connection files through a combination of the tempfile module in the Python standard library and the Create Database Connection Geoprocessing tool.

Your config file could be read in your script, used to make a temporary connection file, establish your Query Layers, and then delete the connection file.

Jason Scheirer
  • 18,002
  • 2
  • 53
  • 72
  • 3
    I was really hoping to not be tied to the file system at all. That prospect doesn't look very good; does it? one of the reasons I don't like working with ESRI. They make it so hard to develop a clean, simple solution. – jpmc26 Feb 11 '14 at 00:35
  • 1
    Worth noting that the Create Database Connection tool requires ArcEditor/Standard or ArcInfo/Advanced license levels only and is not available for ArcView/Basic licenses. A workaround is to create the connection using ArcObjects via python and comtypes. – user2856 Feb 11 '14 at 00:38
  • It should be noted that using tempfile to actually create the file may be ill-advised. It does a lot of crazy stuff with permissions and visibility of the file and may obtain a write-lock on the file. That may or may not be a problem; I didn't check. I just used gettempdir() and made my own file in there and am deleting it in a finally block at the end of my program. (I'm also deleting it before creating the connection if it exists, just to be safer.) – jpmc26 Feb 11 '14 at 20:44