Possible to create tableview in memory and fill

1735
2
03-18-2014 01:01 PM
AlexanderGray
Occasional Contributor III
Looking to use management AddAttachment tool in ArcPy.  Want to add a bunch of attachments without writting a csv file.  Basically reading files from disk, creating the row and adding the attachments.  Bulk process with 100s thousands of files, don't want to write a csv file on disk with all the entries.  Want to create the tableview in memory and populate it with values and use it in the addattachment.
Any one knows if it can be done
Tags (2)
0 Kudos
2 Replies
MattEiben
Occasional Contributor
I've unfortunately never had the chance to fiddle with the AddAttachment tool due to a lack of licensing, but to create in memory tables in arcpy, you would probably do something along these lines:

# Made your in memory table and add pertinent data
table = arcpy.CreateTable_management("in_memory", tableName)
arcpy.AddField_management(table, "ID", "LONG", 9)
arcpy.AddField_management(table, "NAME", "TEXT")

# Insert your data
fields = ["ID","NAME"] 
with arcpy.da.InsertCursor(table,fields) as insertcursor:
    for row in dataToInsert:
        insertcursor.insertRow(row)

# Some tools may require a table view ...
arcpy.MakeTableView_management(table, "table_view")

# Make sure to clean up your workspace, especially for larger batch processes
arcpy.Delete_management(table)
arcpy.Delete_management("table_view")


I'm not sure how this would work with the AddAttachment tool, but this should give a general idea on creating in_memory tables in arcpy.  Hope it helps!
0 Kudos
JamesCrandall
MVP Frequent Contributor
This works for me.


  if arcpy.Exists("in_memory\\matchtable"):
    arcpy.Delete_management("in_memory\\matchtable")
    
  ## setup for the attachments
  input = r"H:\Documents\ArcGIS\Default.gdb\InstantValues"
  inputField = "InstantValues_staname"
  matchTable = arcpy.CreateTable_management("in_memory", "matchtable")
  matchField = "STATION"
  pathField = "Picture"
  arcpy.AddField_management(matchTable, matchField, "TEXT")
  arcpy.AddField_management(matchTable, pathField, "TEXT")
  picFolder = str(outfolder)

  ## check to see if matchtable exists and remove attachements if it does
    
  fields = ["STATION", "Picture"]
  cursor = arcpy.da.InsertCursor(matchTable, fields)
  ##go thru the picFolder of .png images to attach 
  for file in os.listdir(picFolder):
    if str(file).find(".png") > -1:
       pos = int(str(file).find("."))
       newfile = str(file)[0:pos]
       cursor.insertRow((newfile, file))
  del cursor

  # the input feature class must first be GDB attachments enabled
  arcpy.EnableAttachments_management(input)
 
  # use the match table with the Add Attachments tool
  arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
  arcpy.AddMessage("Attachments enabled/created")

0 Kudos