Just when I thought I had cursors figured out....
I have a small test table (4 records) that I apply arcpy.MakeTableView_management to. Then I run a simple search cursor:
>>> fields = ["Type","StreetName"]
>>> with arcpy.da.SearchCursor("view",fields) as cursor:
... for row in cursor:
... if "'{}'".format(row[0]) == "'{}'".format("Junk"):
... print "'{},{}'".format(row[1],row[0])
...
'BUCKMAN,Junk'
The print statement returns the one row that meets the conditional. Cool....
But I want to select that one single record. If I swap out the print statement with :
arcpy.SelectLayerByAttribute_management("view","NEW_SELECTION")
All 4 records get selected. In fact if I run it like this:
>>> with arcpy.da.SearchCursor("view",fields) as cursor:
... for row in cursor:
... if "'{}'".format(row[0]) == "'{}'".format("Junk"):
... print "'{},{}'".format(row[1],row[0])
... arcpy.SelectLayerByAttribute_management("view","NEW_SELECTION")
...
'BUCKMAN,Junk'
You can see it prints just the fields from the record that meets the conditional, but all four records get selected. It's gotta be a (simple) Joe thing, but I'm missing it....
Solved! Go to Solution.
Your select by attribute needs a where clause added.
Your select by attribute needs a where clause added.
Cursors are aware of selections on views and layers, but the tools that select records on views and layers are not aware of cursors. If you create a new selection on a view or layer with no SQL clause, it selects all the records.
Thanks guys. I knew it was a Joe oversight.