Another da.SearchCursor Issue

223
3
Jump to solution
11-15-2017 09:04 AM
JoeBorgione
MVP Esteemed Contributor

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....

Dan Patterson

Joshua Bixby

That should just about do it....
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Thanks guys.  I knew it was a Joe oversight.  

That should just about do it....
0 Kudos