Database connections in arcgispro-py3

3610
2
Jump to solution
03-13-2018 09:27 AM
JohnBroecher
New Contributor III

Since Pro stores database connections in a project, how do we connect to a database from within a standalone python script? 

For example below is how I would have previously connected in ArcGIS 10.5

import arcpy

GDB_directory = r"C:\Python Testing"

targetEntGDB = r"Database Connections/espr1sql14.sde/"

extractedGDB ="600150be54a84779a461d4fc37247f91.gdb"

extractedGDB = GDB_directory + "\" +extractedGDB

arcpy.Copy_management(extractedGDB, targetEntGDB)

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

In ArcGIS Desktop/ArcMap, "Database Connections" is an application alias for the ArcCatalog folder in a users roaming application data directory.  I say it is an application alias because only the application and its tools are aware of it.  Although ArcGIS Pro still has roaming application data directories for users, database connection files are not stored their unless they have been marked as "Favorites," and even then it is a copy at a point in time of the original file.

Although "Database Connections" worked in ArcMap, I would argue the best practice is to give a fully-qualified path to the database connection file and not rely on a user-specific, application-level alias.  If you are using fully-qualified paths, the tools will work the same between ArcMap and ArcGIS Pro.

If you want to access a database connection file that is part of a project, the database connection files are stored in the project's "home folder" which can be accessed via the homeFolder property of an ArcPy Mapping project:

import arcpy
import os

aprx_file = # path to aprx file
GDB_directory = r"C:\Python Testing"

aprx = arcpy.mp.ArcGISProject(aprx_file)
targetEntGDB = os.path.join(aprx.homeFolder, "espr1sql14.sde")
extractedGDB ="600150be54a84779a461d4fc37247f91.gdb"
extractedGDB = os.path.join(GDB_directory, extractedGDB)
arcpy.Copy_management(extractedGDB, targetEntGDB)

Note:  You really want to use os.path.join to build paths.

View solution in original post

2 Replies
by Anonymous User
Not applicable

Can you find the true path for the database connection?

Pro Projects are usually stored here: 

C:\Users\XXX\Documents\ArcGIS\Projects

Open one of the project folder to see the .sde file. Hold down the shift button on they keyboard, right click the .sde file and copy it's path to add in the python script.

JoshuaBixby
MVP Esteemed Contributor

In ArcGIS Desktop/ArcMap, "Database Connections" is an application alias for the ArcCatalog folder in a users roaming application data directory.  I say it is an application alias because only the application and its tools are aware of it.  Although ArcGIS Pro still has roaming application data directories for users, database connection files are not stored their unless they have been marked as "Favorites," and even then it is a copy at a point in time of the original file.

Although "Database Connections" worked in ArcMap, I would argue the best practice is to give a fully-qualified path to the database connection file and not rely on a user-specific, application-level alias.  If you are using fully-qualified paths, the tools will work the same between ArcMap and ArcGIS Pro.

If you want to access a database connection file that is part of a project, the database connection files are stored in the project's "home folder" which can be accessed via the homeFolder property of an ArcPy Mapping project:

import arcpy
import os

aprx_file = # path to aprx file
GDB_directory = r"C:\Python Testing"

aprx = arcpy.mp.ArcGISProject(aprx_file)
targetEntGDB = os.path.join(aprx.homeFolder, "espr1sql14.sde")
extractedGDB ="600150be54a84779a461d4fc37247f91.gdb"
extractedGDB = os.path.join(GDB_directory, extractedGDB)
arcpy.Copy_management(extractedGDB, targetEntGDB)

Note:  You really want to use os.path.join to build paths.