I would like to run a script on a ArcSDE feature class. The script runs perfectly when I hard code a shapefile, but is unable to connect to an ArcSDE database. The purpose of the script is to assign odd valued UIDs, and generate a dbf as an output with selected fields.

import arcpy
from arcpy import management as dm
arcpy.env.overwriteOutput = True

# Set environment path to SDE connection
arcpy.env.workspace = r"C:\Users\client\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\OZ@DB_MAPS.sde"

# Define fc name, DBF output location, and DBF output name
fc = "DB.OZ.PlantCenter"
gisID = "GIS_2016_0"
dbfLocation = r"C:\Users\client\data"
dbfOutputName = "trees_GIS"

if arcpy.Exists(fc):
    arcpy.AddMessage("The feature class exists")
    arcpy.AddError("The feature class does not exist")

with arcpy.da.UpdateCursor (fc, gisID) as cursor:
    for row in cursor:
        if row[0] == None:
            row[0] = 0

# Query out odd values in "GIS_2016_0"
odd = []
even = []
oddCursor = arcpy.SearchCursor(fc)
for row in oddCursor:
    uid = row.getValue (gisID)
    if uid %2 == 1:
        odd.append (uid)
        even.append (uid)

field = arcpy.AddFieldDelimiters(fc,gisID)
odd_sql = ' OR '.join('%s = %s' %(field,i) for i in odd)
dm.MakeFeatureLayer(fc, "fc_temp")
dm.SelectLayerByAttribute("fc_temp", "NEW_SELECTION", odd_sql)

# Search for the latest maximum odd value
maxCursor = arcpy.da.SearchCursor("fc_temp", gisID)
FirstRecord = True
for row in maxCursor:
    if FirstRecord:
        FirstRecord = False
        maxValue = int(row[0])
        maxValue = max(int(row[0]),maxValue)

if (maxValue % 2 == 1): #odd
    # Define the function to assign incrementing odd values
    def autoIncrement (start=0, step=1):
        i = start
        while 1:
            yield i

    # Query for new attributes with no assigned unique ID, and populate with a number following the last largest odd value
    zero_qry = '"' + gisID + '" = ' + "0"
    dm.MakeFeatureLayer(fc, "fc_temp")
    dm.SelectLayerByAttribute("fc_temp", "NEW_SELECTION", zero_qry)
    incrementCursor = arcpy.UpdateCursor ("fc_temp")
    nextValue = maxValue + 2
    incrementer = autoIncrement (nextValue, 2)
    for row in incrementCursor:
        row.setValue(gisID, incrementer.next())
    arcpy.AddMessage("No odd unique IDs found.")

# Delete previous dbfs and export the feature class as a dbf with selected fields (overwrite)
dbfPath = dbfLocation + '\\' + dbfOutputName + '.dbf'
if arcpy.Exists(dbfPath):
arcpy.TableToTable_conversion (fc, dbfLocation, dbfOutputName) 

# Get all fields in dbf and remove unwanted fields
fields = arcpy.ListFields("fc_temp") 
keepFields = ['GIS_2016_0', 'SourceAcce', 'AccessionC', 'SectionNam', 'Longitude', 'Latitude']
dropFields = [x.name for x in fields if x.name not in keepFields]
dm.DeleteField (dbfPath, dropFields)

When the client ran this script through task scheduler using a basic task, the script returned no errors, but no output was produced.

New edit 01/13/2017:

The client ran the script in Arc, and was provided this error message:

enter image description here

I believe this SDE layer is versioned, so it might be related to the issue here: https://geonet.esri.com/thread/88268

Any thoughts on how to resolve the code?

  • 1
    This particular script does nothing. Can you provide a bit more of the script? If no output is generated you'd get an error or maybe its creating an empty output? (that'd generate a warning). Theres a lot of different things it could be: 32vs64bit (do you have the right dbms drivers?). What software are you using to execute the script? ArcMap, Server, etc? Does the script work when run inside ArcMap? In short, you'll need to supply more details otherwise its just guess work here. – KHibma Jan 9 '17 at 19:25
  • Thanks for the questions. I've included all parts of the script. On my computer, the script ran sucessfully on PythonWin and through ArcMap, The client is running the script through task scheduler, and hasn't tried on ArcMap. – saoirse Jan 9 '17 at 19:32
  • 1
    It looks like you're relying on the workspace + fc name to drive the input FC. I would have expected an error at some point if it couldnt be found. I think you're going to have to do some `arcpy.AddMessage("step X") to start tracing it. You can also do arcpy.Exists(fc) inside an AddMessage to help determine if data is found or not – KHibma Jan 9 '17 at 19:54
  • 1
    The key here is that the script is being run as a scheduled task. Please edit your question to specify the exact Windows Task Scheduler options/parameters used when the task was created (with screenshots if possible). – user2856 Jan 9 '17 at 23:43
  • 1
    IF you are using windows authentication in your connection file to your SQL RDBMS, make sure the scheduled task is using credentials that exist in the database and have access to the data. Also, your output is currently to r"U:\Users\George..." Make sure the scheduled task user credentials have access to this folder location. You will need to generate some logging to troubleshoot this further. – Ben S Nadler Jan 10 '17 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.