HOW TO

Create a database connection string for use in a Python script

Last Published: October 5, 2023

Summary

Instructions provided describe the steps to specify a valid Python syntax for a database connection file string. The steps demonstrate how to use ModelBuilder to generate the string automatically.

Procedure

  1. In ArcCatalog, create a new toolbox, and within it, a new model. ModelBuilder opens automatically.
  2. 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.

[O-Image]

  1. 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.

[O-Image]

    • 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'.

[O-Image]

  1. Close the tool dialog, leaving the model open. If the required fields are filled with valid data, the model tools are considered validated.

[O-Image]

  1. From the Model menu, click Export > To Python Script.
  2. 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.

[O-Image]

If the script is to be used outside of ArcMap or ArcCatalog, please refer to KB41592 for information on formatting the connection file string.

Article ID:000012683

Software:
  • ArcMap

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options