cursor = arcpy.SearchCursor(tablepath)
Solved! Go to Solution.
import arcinfo import arcpy import sys def main(): global mosaicDb global pathtable pathtable = '%SCRATCHWORKSPACE%/Scratch.gdb/Paths' mosaicDb = '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/' inputmosaic = arcpy.GetParameterAsText(0) objectid = arcpy.GetParameterAsText(1) arcpy.AddMessage('Input Mosaic Dataset: ' + mosaicDb + inputmosaic) arcpy.AddMessage('Search SourceObjectID: ' + objectid) deletePathDb() exportCatalog(inputmosaic) fs = getMosaicRow(objectid, pathtable) arcpy.SetParameter(0, fs) def deletePathDb(): try: arcpy.Delete_management(pathtable) arcpy.AddMessage('table deleted successfully') except: #catch because will throw error if table not there arcpy.AddMessage('Error attempting delete path table') arcpy.AddMessage(arcpy.GetMessages()) def exportCatalog(name): try: fullname = '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/' + name arcpy.ExportRasterCatalogPaths_management(fullname, "ALL", pathtable) except Exception as e: arcpy.AddMessage("Error export catalog") arcpy.AddMessage(arcpy.GetMessages()) def getMosaicRow(objectId, table): where = 'SourceOID = ' + objectId #this is line that explodes cursor = arcpy.SearchCursor(table, where) fs = None for row in cursor: fs = row return fs
I am suspicious of your query to open the cursor. Is ObjectID a string? If not it needs to be cast to a string.
What happens if the result is no records?
Add some print statements to print out what is returned.
I have never used the row variable in a cursor, always row.attribute, what does row return?
Update and Insert cursors work differently and since you haven't added a new row it probably just does nothing.
You also need to close the cursor but I suppose that you are relying on the local variables in the function. I would still explicitly 'del cursor'
You may have more success by rearranging your whole script. I would avoid opening and reopening a cursor just to get one record.
Maybe get all the values in one pass and put into a python dictionary? That would be much faster.
cursor = arcpy.SearchCursor(table, where)
# testbug.py import arcinfo import arcpy import sys def main(): ## global mosaicDb ## global pathtable # only a geoprocessing service can do this: # pathtable = '%SCRATCHWORKSPACE%/Scratch.gdb/Paths' # Desktop equivalent 9.3+ installTemp = arcpy.GetSystemEnvironment("ARCTEMPDIR") arcpy.env.scratchWorkspace = installTemp+"/scratch.gdb" if not arcpy.Exists(arcpy.env.scratchWorkspace): print "failed to set scratch workspace" sys.exit() pathtable = arcpy.env.scratchWorkspace +"/Paths" print pathtable mosaicDb = r'D:\data\aerial\catalog\nzmgcat.gdb' # '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/' ## inputmosaic = arcpy.GetParameterAsText(0) ## objectid = arcpy.GetParameterAsText(1) inputmosaic = 'nztm1' objectid = '40' arcpy.AddMessage('Input Mosaic Dataset: ' + mosaicDb + "/" + inputmosaic) # bug fix added separator arcpy.AddMessage('Search SourceObjectID: ' + objectid) print 'Input Mosaic Dataset: ' + mosaicDb + "/" + inputmosaic print 'Search SourceObjectID: ' + objectid deletePathDb(pathtable) exportCatalog(inputmosaic,pathtable,mosaicDb) fs = getMosaicRow(objectid, pathtable) print fs arcpy.SetParameter(3, fs) # Not 0, this would have to be at least the third parameter def deletePathDb(pathtable): try: if arcpy.Exists(pathtable): arcpy.Delete_management(pathtable) arcpy.AddMessage('table deleted successfully') print 'pathtable deleted successfully' else : print 'pathtable does not exist' except: #catch because will throw error if table not there print "error in deletePathDb" arcpy.AddMessage('Error attempting delete path table') arcpy.AddMessage(arcpy.GetMessages()) def exportCatalog(name,pathtable,mosaicDb): try: fullname = mosaicDb + '/' + name arcpy.ExportRasterCatalogPaths_management(fullname, "ALL", pathtable) except Exception as e: arcpy.AddMessage("Error export catalog") arcpy.AddMessage(arcpy.GetMessages()) def getMosaicRow(objectId, table): where = 'SourceOID = ' + objectId # this is my objectid field name #this was line that explodes cursor = arcpy.SearchCursor(table, where) fs = None for row in cursor: # fs = row # this will be a geoprocessing row object of no value outside the cursor sPath = row.path nSourceOID = row.SourceOID nOID = row.OID del cursor return (nOID,nSourceOID,sPath)# return a tuple which contains the field contents if __name__ == '__main__': main()
c:\workspace/scratch.gdb/Paths Input Mosaic Dataset: D:\data\aerial\catalog\nzmgcat.gdb/nztm1 Search SourceObjectID: 40 pathtable deleted successfully (40, 40, u'D:\\data\\aerial\\catalog\\test\\r12a.jpg')