Python GPService SDE Connection

3729
6
06-07-2013 05:45 AM
BrianLeroux
Regular Contributor
I am trying to publish a GPService from a python script. I am having with the service connecting to SDE. I was having trouble so I added the Test Connection section to see what path was being used. It seems like it is not getting the connection info from the SDE file. The script runs fine in ArcMap and I have the SDE file in the same location on the server as it is my dev machine. Any ideas?

"Error executing tool.: C:\arcgisserver\directories\arcgissystem\arcgisinput\GPTools\WildfireUpload.GPServer\extracted\v101\\ArcSDE.dbo.WILDFIREPERIM Table does not exist "

import arcpy, os, zipfile
from arcpy import env

#Setting the env workspace to be an SDE geodatabase
ws = env.workspace = "D:\\agsResources\\DataStoreSDEs\\GIS01(TEST Server).sde"
#Test Connection
if arcpy.Exists(ws + "\\ArcSDE.dbo.WILDFIREPERIM") == False:
    arcpy.AddMessage("Table does not exist")
    arcpy.AddError(ws + "\\ArcSDE.dbo.WILDFIREPERIM Table does not exist")
else:
    arcpy.AddMessage("Table Exists")

infile = arcpy.GetParameterAsText(0)
#startdate = arcpy.GetParameterAsText(1)
outpath, outfileext = os.path.splitext(infile)
filename = outpath.split('\\')[-1]
try:
    # unzip file
    fireZip = zipfile.ZipFile(infile, 'r')
    fireZip.extractall(outpath)
    fireZip.close()
    #arcpy.SetParameterAsText(1,outpath + "\\" + filename + ".shp")
    shpPath = outpath + "\\" + filename + ".shp"
    arcpy.AddMessage("Finished unzipping file.")

# Local variables:
    WildFire_Table_Target = ws + "\\ArcSDE.dbo.WILDFIREPERIM"
    # Create FieldMappings object and load the target dataset
#
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(WildFire_Table_Target)

    inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required]
    for inputfield in inputfields:
        # Iterate through each FieldMap in the FieldMappings
        #
        for i in range(fieldmappings.fieldCount):
            fieldmap = fieldmappings.getFieldMap(i)
            #arcpy.AddMessage(fieldmap.getInputFieldName(0))
            # If the field name from the target dataset matches to a validated input field name
            #
            if fieldmap.getInputFieldName(0) == inputfield.replace("", ""):
                # Add the input field to the FieldMap and replace the old FieldMap with the new
                #
                fieldmap.addInputField(shpPath, inputfield)
                fieldmappings.replaceFieldMap(i, fieldmap)
                break

            
# Process: Append
    arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings)

except Exception as e:
      print e.message
      arcpy.AddError(e.message)
0 Kudos
6 Replies
BrianLeroux
Regular Contributor
So I stopped trying to use the workspace and I am just setting a variable with the path. This now shows the table exists.

WildFire_Table_Target = r'D:\agsResources\DataStoreSDEs\HOCCSQLAH01_AHREPORT(TEST Server).sde\ArcSDE.dbo.WILDFIREPERIM'

However when trying to add the table to fieldmapping I get an error.
fieldmappings.addTable(WildFire_Table_Target)

Error executing tool.: FieldMappings: Error in adding table to field mappings Failed to execute (ImportWildfirePerim). Failed to execute (ImportWildfirePerim).

Doesn't make sense to me as it has no issues with the filed mapping in ArcMap.
0 Kudos
KevinSadrak
New Contributor III

Did you find a solution to this?  I am having the same issue.  I believe it is permissions wiht ArcGIS Server, but I'm not sure where to make the changes.

0 Kudos
BrianLeroux
Regular Contributor

Kevin-

I did get this to work but it was a while ago so I am having difficulty remembering everything i did to get this working. For the connection I had to use the following line to get this to work. You should also not that I use windows authentication and my gis server account needed write access to the SQL table that i was updating.

# Local variables:

    WildFire_Table_Target = "Database Connections\\GEOSQL01_ArcSDE(PROD Server).sde\\ArcSDE.dbo.WILDFIREPERIM"

Hope that helps..

0 Kudos
KevinSadrak
New Contributor III

Brian,

Thanks for the reply. 

In order for your code to work, you had to put the .SDE in the
Database Connections, that is interesting.  I am creating a new .sde in my
script that will allow for version changes, I suppose I could try and move the
generated .sde to the Database Connections folder and see if that works. 

I’ll post my findings!

-Kevin

0 Kudos
MichaelRobb
Regular Contributor II

and?  never posted your findings...

0 Kudos
BrianLeroux
Regular Contributor

Here is an update on my end. I ended up having success placing the sde connection file in the same directory as the script and then referencing that with a system path variable. Here is a sample. This has been the most consistent method for me.

#Get script path

script_path = sys.path[0]

#Path to SDE using new script path variable

sdeconn = os.path.join(script_path, "SQLDB.sde")

#Path to feature class

polfc = os.path.join(sdeconn, 'ArcSDE.dbo.MyData)

0 Kudos