python script using arcpy.CreateDatabaseConnection_management creates a connection to the wrong geodatabase.

8951
5
Jump to solution
09-18-2014 11:23 AM
JuliePaine
New Contributor II

I'm using SQL Server 2008 R2, sde v10 geodatabases, and Desktop v10.2.2.

'PublicWorks'  geodatabase exists in the sde schema, 'GIS' geodatabase in the dbo schema. Running the following script establishes a connection to 'PublicWorks' instead of 'GIS'.

Using the tool in ArcToolbox establishes the connection correctly to the GIS db.

Why is the attached script not connecting to the GIS geodatabase?

0 Kudos
1 Solution

Accepted Solutions
ChristianWells
Esri Regular Contributor

Hi Julie, can you try the following script:

#create a new sde connection

#List all featureclasses in a geodatabase, including any within feature datasets

import arcpy,os

from arcpy import env

arcpy.env.overwriteOutput = True

arcpy.CreateDatabaseConnection_management(out_folder_path="Database Connections",

                                          out_name="GIS winauth to gis-server.sde",

                                          database_platform="SQL_SERVER",

                                          instance="gis-server",

                                          account_authentication="OPERATING_SYSTEM_AUTH",

                                          database="GIS",

                                          version_type="TRANSACTIONAL",

                                          version="dbo.DEFAULT")

arcpy.env.workspace = r"Database Connections\GIS winauth to gis-server.sde"

datasets = arcpy.ListDatasets(feature_type='feature')

datasets = [''] + datasets if datasets is not None else []

for ds in datasets:

    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):

        path = os.path.join(arcpy.env.workspace, ds, fc)

        print path

I believe the problem with the previous script was that the parameters were looking for the data in the following order:

(out_folder_path, out_name, database_platform, instance, {account_authentication}, {username}, {password}, {save_user_pass}, {database}, {schema}, {version_type}, {version}, {date})

Although they are optional parameters, arcpy is expecting a variable there. So another way to complete this is to specify the dictionary parameters for arcpy.CreateDatabaseConnection_management()

View solution in original post

0 Kudos
5 Replies
ChristianWells
Esri Regular Contributor

Hi Julie, I don't see the script attached to this message, can you post it again?

0 Kudos
JuliePaine
New Contributor II

Here's the script....

#create a new sde connection
#List all featureclasses in a geodatabase, including any within feature datasets
import arcpy,os
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.CreateDatabaseConnection_management("Database Connections",
                                          "GIS winauth to gis-server.sde",
                                          "SQL_SERVER",
                                          "gis-server",
                                          "OPERATING_SYSTEM_AUTH",
                                          "GIS",
                                          "TRANSACTIONAL"
                                          "dbo.DEFAULT")

arcpy.env.workspace = r"Database Connections\GIS winauth to gis-server.sde"

datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []

for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        path = os.path.join(arcpy.env.workspace, ds, fc)
        print path

0 Kudos
ChristianWells
Esri Regular Contributor

Hi Julie, can you try the following script:

#create a new sde connection

#List all featureclasses in a geodatabase, including any within feature datasets

import arcpy,os

from arcpy import env

arcpy.env.overwriteOutput = True

arcpy.CreateDatabaseConnection_management(out_folder_path="Database Connections",

                                          out_name="GIS winauth to gis-server.sde",

                                          database_platform="SQL_SERVER",

                                          instance="gis-server",

                                          account_authentication="OPERATING_SYSTEM_AUTH",

                                          database="GIS",

                                          version_type="TRANSACTIONAL",

                                          version="dbo.DEFAULT")

arcpy.env.workspace = r"Database Connections\GIS winauth to gis-server.sde"

datasets = arcpy.ListDatasets(feature_type='feature')

datasets = [''] + datasets if datasets is not None else []

for ds in datasets:

    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):

        path = os.path.join(arcpy.env.workspace, ds, fc)

        print path

I believe the problem with the previous script was that the parameters were looking for the data in the following order:

(out_folder_path, out_name, database_platform, instance, {account_authentication}, {username}, {password}, {save_user_pass}, {database}, {schema}, {version_type}, {version}, {date})

Although they are optional parameters, arcpy is expecting a variable there. So another way to complete this is to specify the dictionary parameters for arcpy.CreateDatabaseConnection_management()

0 Kudos
JuliePaine
New Contributor II

Thank You! That did the trick!

0 Kudos
RemiAmbattakathutt
New Contributor

I am having a similar issue. I am trying to connect to a version other than DEFAULT by providing version parameter. But it is always connecting to FEFAULT version. Could you please provide any input on this? Here is the code i used

arcpy.CreateDatabaseConnection_management(out_folder_path="Database Connections",

                                          out_name="creaconn5.sde",

                                           database_platform="ORACLE",

                                           instance="srv1/testgis",

                                          account_authentication="DATABASE_AUTH",

                                          username="sde",

                                          password="xxxx",

                                          save_user_pass="SAVE_USERNAME",

                                          version_type="TRANSACTIONAL",

                                          version="TEST.VERSION1")

In the immediate window right pane it shows as

Executing: CreateDatabaseConnection "Database Connections" creaconn5.sde ORACLE srv1/testgis DATABASE_AUTH sde ***** SAVE_USERNAME # SDE TRANSACTIONAL # #

You can observe that, even though I specified version parameter, it took it as blank (#)

0 Kudos