How To: Create an database connection string for use in a Python script
Instructions provided describe the steps to specify valid Python syntax for an database connection file string. The steps below show how to use ModelBuilder to generate the string automatically.
- In ArcCatalog, create a new toolbox, and within it, a new model. ModelBuilder opens automatically.
- In the ModelBuilder window, drag in an ArcToolbox tool that can reference the applicable data source. For example, the Make Feature Layer tool or the Copy Raster tool can be used to reference a vector or raster data source in an enterprise geodatabase.
- In the model, double-click the applicable tool to open it, and populate the fields using the browse controls, combo boxes, or other assists where available, otherwise by typing.
The SDE connection files are located either in the Windows user profile folder on in the specified folder location, as follows:
- In the Copy Raster example, the Input Raster parameter specifies an SDE connection file that is in the Windows user profile folder. When making a database connection using the Add Database Connection tool in ArcCatalog, the database connection file (.sde) is saved in the Windows user profile folder.
- In the Make Feature Layer example, the Input Features parameter specifies an database connection file that is not in the Windows user profile but in a folder 'c:/sdeconnections'.
- Close the tool dialog, leaving the model open. If the required fields are filled with valid data, the model tools are considered validated.
- From the Model menu, click Export > To Python Script.
- Edit the exported Python script in a text editor. Find the string that references the data source, copy it, and paste it to your script where needed. In this example, the local variables 'sde1021_MAP_dem_100x_bilinear' and 'sde1021_MAP_Parcels__2_' are string variables containing a path to a connection (.SDE) file.
If the script is to be used outside of ArcMap or ArcCatalog, please refer to KB41592 for information on formatting the connection file string.