arcpy.da.searchcursor with expression

899
5
Jump to solution
05-13-2013 12:04 PM
BKS
by
Occasional Contributor
Just wondering if anyone has had any trouble with using a query expression with arcpy.da.searchcursor?

I've used this successfully many time but I have code where I create table views from tables in a fGDB then use a cursor on selected records using arcpy.da.searchcursor.

One of these works fine while the other does not reduce the number of records from the total records in the table.

I've stared at this for a while and really don't see a reason why one would work and the other does not.

The only think I can think of is that the table view that does NOT work has a joined table associated with it using AddJoin.

Otherwise there is no significant difference that I can see between the two.



One other thing I did try was to using CopyRows to copy the table view to a table, use MakeTableView using the same query expression used above in the arcpy.da.searchcursor, and then use CopyRows to a copy the view to table.  After this runs, the final table copied behaves as if the query was not applied (same as happens in arcpy.da.searchcursor).

any leads appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BKS
by
Occasional Contributor
There is a bug that is fixed at 10.2-

NIM076948- The arcpy.da.SearchCursor() where clause does not work with a table view.


NOTE:  As a work-around I successfully copied the table view to an in_memory table using CopyRows.  This solved the problem since arcpy.da.searchcursor with where clause works if the source is a table as far as I understand the bug.

Brian

View solution in original post

0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
The field names will change when you use the 'AddJoin' tool, and this is most likely the issue.  The new field name will be <feature class/table name>.<field name>.
0 Kudos
BKS
by
Occasional Contributor
The field names will change when you use the 'AddJoin' tool, and this is most likely the issue.  The new field name will be <feature class/table name>.<field name>.


Wish that was the problem but already handle that.  I print the expression and try the same expression on the table manually and it works just fine.

Technically I use CopyRows to copy the table view (that includes the join) to a table in the fGDB.  Then I copy the expression used from the code and it works.  The one think I needed to do was change the "." character to a "_" character since this is what happens when you save a table with a join to a fGDB.  I assume the fGDB doesn't like "." in field names so it converts to "_".
0 Kudos
LucasDanzinger
Esri Frequent Contributor
There is a bug that is fixed at 10.2-

NIM076948- The arcpy.da.SearchCursor() where clause does not work with a table view.
0 Kudos
BKS
by
Occasional Contributor
Thanks - I couldn't think of anything else to try. 

What's strange is that it does work sometimes.

I don't know how to see the contents of the bug so can you tell me if the arcpy.searchcursor works on a table view with a where clause (i.e. is this limited to arcpy.da.searchcursor)?
0 Kudos
BKS
by
Occasional Contributor
There is a bug that is fixed at 10.2-

NIM076948- The arcpy.da.SearchCursor() where clause does not work with a table view.


NOTE:  As a work-around I successfully copied the table view to an in_memory table using CopyRows.  This solved the problem since arcpy.da.searchcursor with where clause works if the source is a table as far as I understand the bug.

Brian
0 Kudos