Select to view content in your preferred language

update an attribute if record found in a query

1857
10
Jump to solution
04-28-2014 02:54 PM
MatthewWalker1
Occasional Contributor
I am trying to update an attribute in one table, if that record is found in a MakeQuery table (I made a table view of all records not yet processed, and once processed, I want to turn the 'Processed' flag to 'Y').

rows = arcpy.UpdateCursor("Assets") for row in rows:  if row.Processed == "N" and row.Asset_ID == "Asset_Unprocessed.Asset_ID":   row.Processed = "Y"   rows.updateRow(row) del row del rows


I am trying to update in the Assets table the 'Processed' flag to 'Y', IF the Assets key is found in the 'Assets_Unprocessed' table.  This does not throw an error, it just does not find and update my test data properly.  The issue is the comparison to the Assets_Unprocessed table (the rest of the code works), and I've tried several different ways to type that table (full path, brackets, etc.) and can't seem to get that part to work.
Tags (2)
0 Kudos
10 Replies
MatthewWalker1
Occasional Contributor
Booya!  It finally worked!  I noticed that in Jake's last reply that in the SearchCursor function in the append command it reads "...(row.Asset_ID)" rather than "...auRows.Asset_ID)" and that allowed me to get through that bit of code.  The rest of Jake's code for running the UpdateCursor worked and the attribute 'Processed' flag did get changed to 'Y'!  thanks a million guys!

The one thing I am noticing though, is that I cannot run my entire code all at once (i.e. I can't copy and paste the whole code in the ArcMap Python screen) or I will get an error stating, "Runtime error <type 'exception.NameError'>: name 'row' is not defined".  If I run all the code down to the 'del row, rows' section, then after everything runs run that code, no errors, and then I can paste in the UpdateCursor section with no errors, just not all at the same time.  I will eventually want to run this from an executable or in batch and not within ArcMap, and am wondering if I will get the same error or not.  Any ideas why this error occurs with the whole code, but not if fed in sections?

Again, I really appreciate all the help on this!  Now I just have to work on how to make these records spatial, at their measure values, a whole other can of worms!

FYI: final code that seems to work for me, at least piecemeal:
import arcpy
from arcpy import env
import datetime
import os
env.workspace = "c:\Users\matt\Desktop\Projects\Assets\Assets.mdb"

#Make a TableView of Non-Spatial Asset records that need to be processed
arcpy.MakeQueryTable_management (["Assets"], "Asset_Line", "ADD_VIRTUAL_KEY_FIELD", "",[["Asset_ID"], ["Ditch_ID"], ["Begin_Station"], ["End_Station"]], "[Assets.Processed] <> 'Y'")

#Add queried data to Assets_Unprocessed table-to be used by ArcObjects utility to create spatial features
arcpy.Append_management ("Asset_Line", "Assets_Unprocessed")

#Copy attribute data to spatial Asset features

#Call ArcObjects utility to snap 'out of ditch' linear features to spatial Asset features

#Call ArcObjects utility to snap related assets to spatial Asset features

au = "Assets_Unprocessed"
lstIds = []
fld = "Asset_ID"

auRows = arcpy.SearchCursor(au)
for row in auRows:
 lstIds.append(row.Asset_ID)
del row, auRows


table2 = "Assets"
for ID in lstIds:
 rows = arcpy.UpdateCursor(table2)
 for row in rows:
  if row.Asset_ID == ID:
   row.Processed = 'Y'
   rows.updateRow(row)
0 Kudos