da cursor behaviour on layers with where clause

721
7
Jump to solution
07-16-2013 09:24 AM
MathewCoyle
Frequent Contributor
I can't seem to find any reference to this in help or forums, but it seems a da cursor cannot apply a where clause to a layer with a where clause already applied. Does anyone know if this is this working as intended behaviour or a bug?

Here is some test code where this occurred.

import arcpy  data = r'D:\GIS\DataBase\misc.gdb\CLI_N' lyr = 'temp' arcpy.MakeFeatureLayer_management(data, lyr, 'OBJECTID < 100') print(arcpy.GetCount_management(lyr)) count = 0 cursor = arcpy.da.SearchCursor(lyr, 'OBJECTID', 'OBJECTID < 10') for _ in cursor:     count += 1 print(count)


This code returned
>>>  99 99
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Verified bug that has been fixed in 10.2.
NIM076948 The arcpy.da.SearchCursor() where clause does not work with a table view


Work around for 10.1 involved using a select by attribute on the layer with the where clause then running a cursor without a where clause. So editing the initial example that was broken.

import arcpy  data = r'D:\GIS\DataBase\misc.gdb\CLI_N' lyr = 'temp' arcpy.MakeFeatureLayer_management(data, lyr, 'OBJECTID < 100') print(arcpy.GetCount_management(lyr)) count = 0 arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', 'OBJECTID < 10') cursor = arcpy.da.SearchCursor(lyr, 'OBJECTID') for _ in cursor:     count += 1 arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION') print(count)


Returns the appropriate.
>>>  99 9 >>> 

View solution in original post

0 Kudos
7 Replies
RhettZufelt
MVP Frequent Contributor
do you only have 99 features in that fc?

I can't seem to get the where clause to take on my FC, it always returns all records in the data.

It seems to work if I put my where clause on a different field, but can't get it to honor a where clause on OBJECTID ?????

R_
0 Kudos
MathewCoyle
Frequent Contributor
do you only have 99 features in that fc?

I can't seem to get the where clause to take on my FC, it always returns all records in the data.

It seems to work if I put my where clause on a different field, but can't get it to honor a where clause on OBJECTID ?????

R_


No, I have almost 3000 features in the FC.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Weird,  if I calculate my "other" field equal to OBJECTID, it won't honor that now either.  Set it back to sequential numbers and it works.  However, the makefeaturelayer never seems to report the same number of features that the same select query, on the same dataset in ArcMap.

Makefeature either seems to ignore my query and returns all results (as if I use OBJECTID) or, will return fewer results than actually match the query.  (Finds 129 where "rec" <= 150 after sequentially calculating the rec field from 1 to 2110)  Exact same "select by attributes" in ArcMap returns the 150 that actually match the query.

R_
0 Kudos
MathewCoyle
Frequent Contributor
Weird,  if I calculate my "other" field equal to OBJECTID, it won't honor that now either.  Set it back to sequential numbers and it works.  However, the makefeaturelayer never seems to report the same number of features that the same select query, on the same dataset in ArcMap.

Makefeature either seems to ignore my query and returns all results (as if I use OBJECTID) or, will return fewer results than actually match the query.  (Finds 129 where "rec" <= 150 after sequentially calculating the rec field from 1 to 2110)  Exact same "select by attributes" in ArcMap returns the 150 that actually match the query.

R_


Well that's even weirder.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Though, I now see a bunch of weird python stuff going on now.

I'd better re-test this after a re-boot......  Especially since I know I have had the MakeFeatureLayer working properly in the past..

More results later,

R_
0 Kudos
MathewCoyle
Frequent Contributor
Verified bug that has been fixed in 10.2.
NIM076948 The arcpy.da.SearchCursor() where clause does not work with a table view


Work around for 10.1 involved using a select by attribute on the layer with the where clause then running a cursor without a where clause. So editing the initial example that was broken.

import arcpy  data = r'D:\GIS\DataBase\misc.gdb\CLI_N' lyr = 'temp' arcpy.MakeFeatureLayer_management(data, lyr, 'OBJECTID < 100') print(arcpy.GetCount_management(lyr)) count = 0 arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', 'OBJECTID < 10') cursor = arcpy.da.SearchCursor(lyr, 'OBJECTID') for _ in cursor:     count += 1 arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION') print(count)


Returns the appropriate.
>>>  99 9 >>> 
0 Kudos
RhettZufelt
MVP Frequent Contributor
Thanks for the update.

Sorry I didn't get back to my testing.  This fell off the burner.

R_
0 Kudos