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)
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..
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!
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
#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)