SearchCursor do not loop after select layer with empty result

263
10
2 weeks ago
Highlighted
by
New Contributor II

I have noticed that after select layer by location/attribute with empty result (example, i tried select layer with attribute, which that do not have), SearchCursor do not see rows in layer table. I tried SearchCursor layer write with dataSource, it's work on *.gdb whom i have physical access, but for *.sde whom i do not have physical access it's do not work ('Cannot open file' error). If i can select all layers features, than SearchCursor will see rows. I know about arcpy.SelectLayerByAttribute, but it is slow. I work with setSelectionSet and after clear selection with setSelectionSet('new', []) is same problem. How me revive layer after select with empty result?

Please, help me and sorry for my English!

Reply
0 Kudos
10 Replies
Highlighted
MVP Frequent Contributor

You need to share your code if you want people to help. Make sure it's formatted correctly

Reply
0 Kudos
Highlighted
by
New Contributor II

after clear selection

 

arcpy.mapping.Layer('TreesA').setSelectionSet('new', [])

 

SearchCursor do not loop rows (result is emty)

 

with arcpy.da.SearchCursor('TreesA', ['OBJECTID']) as rs:
...     for r in rs:
...         print r[0]

 

I know about

 

arcpy.SelectLayerByAttribute_management('TreesA', 'clear_selection')

 

 but this is slow

when i write

 

with arcpy.da.SearchCursor(arcpy.mapping.Layer('TreesA').dataSource, ['OBJECTID']) as rs:
...     for r in rs:
...         print r[0]

 

SearchCursor see rows (print all OBJECTID)

but when use on *.sde, whom i do not have access

i get:

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
RuntimeError: cannot open 'K:\INSTRUKCIJAS\!SDE\backup_connections\PRODSQL_TOPO10_3421-25.sde\PRODSQL_TOPO10.DBO.TOPO10_3\PRODSQL_TOPO10.DBO.TreesA'

Highlighted
MVP Regular Contributor

If the ObjectID field is what you're getting, try using the OID@ field name token instead.

Reply
0 Kudos
Highlighted
by
New Contributor II

Yes, i try get features OID list (field name OBJECTID).

Sorry, i am not understood, can you write example!

Reply
0 Kudos
Highlighted
MVP Regular Contributor
with arcpy.da.SearchCursor(arcpy.mapping.Layer('TreesA').dataSource, ['OID@']) as rs:
Reply
0 Kudos
Highlighted
by
New Contributor II

Sorry, nothing changes!😔

Reply
0 Kudos
Highlighted
by
New Contributor II

You can try

arcpy.SelectLayerByAttribute_management('layer', 'new_selection', 'select by attributes who do not exist in layer')

after this arcpy tools (Search\UpdateCursor, GetCount, SelectLayerByAttributes(with subset_selection method) or SelectLayerByLocation....) do not see rows

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

@JanisV wrote:

after this arcpy tools (Search\UpdateCursor, GetCount, SelectLayerByAttributes(with subset_selection method) or SelectLayerByLocation....) do not see rows


This is expected behavior so I am not exactly sure what you are trying to get at with this comment.  Most, if not all, geoprocessing tools honor selection sets on layers and table views.  If you have a selection set with no records, there is nothing for the tools to operate on.

Highlighted
MVP Esteemed Contributor

Layer.setSelectionSet has some odd behaviors and Esri's documentation is wrong.  Passing an empty list with "New" doesn't clear the selection but creates a new selection with no records, hence why the search cursor is not working.  You need to pass an empty set with "New" to have it clear the selection.

Reply
0 Kudos