how to check if cursor object is empty?

35612
20
02-02-2012 06:15 AM
kyleturner
New Contributor III
It seems an easy task, but I can't figure out how to check if the searchcursor object is empty.
I know that it is empty because the query fails....i.e. arcpy.searchcursor(FC, query).
However, even if it (arcpy.searchcursor..) fails, it still returns a geoprocessing cursor object...

rows = gp.SearchCursor(crosswalk_dbf,query)

print rows ---> returns
<geoprocessing cursor object object at 0x029C7CC0>


I can't check if it's empty, because, well, it's not.
And for reasons I obviously don't understand.......

rows = gp.SearchCursor(crosswalk_dbf,query)
for row in rows:
  if not row.isNull(fieldname)

------------

is always 'true'.

Please help.

I'm using Arc10 SP3, win 7 64bit.

Many thanks.
Tags (2)
0 Kudos
20 Replies
MathewCoyle
Frequent Contributor
I am sure there is a more elegant way, but you could do something simple like...
checkvar = "cursor empty"
rows = arcpy.SearchCursor(fc, query)
for row in rows:
    if checkvar == "cursor empty":
        checkvar = "cursor full"
print checkvar



Here's something probably better, early morning brain not working yet.
row = None
rows = arcpy.SearchCursor(fc, query)
for row in rows:
    #cursor things
if not row:
    print "cursor was empty"

ChrisSnyder
Regular Contributor III
Maybe this would help?

arcpy.MakeFeatureLayer_management(indxTileFC, indxTileFL, "HCPUNIT_NM <> 'OESF'); showGpMessage()
if int(arcpy.GetCount_management(indxTileFL).getoutput(0)) == 0:
   print "Query returned no records!"
else: #if there are records returned by the query, then run a search cursor and look for Null field values in MY_FIELD
   searchRows = arcpy.SearchCursor(indxTileFC, "HCPUNIT_NM <> 'OESF')
   for searchRow in searchRows:
      if searchRow.MY_FIELD == None: #Another way: if searchRow.isNull("MY_FIELD") == True:
         print "MY _FIELD is Null!"
   del searchRow, searchRows
0 Kudos
kyleturner
New Contributor III
Thanks for the help Mathew. (Chris I haven't tried your suggestion yet.)

Still no go.
I even tried a very basic search from the Python cursor. Here's what I tried:

> row, rows = None, None
> query = "Fieldname = '" + "'" variable + "'" --- The quotes are right in my code I know for sure, maybe not here tho.
> table = "C:/path/to/data.dbf
> rows = row.searchcursor(table, query)
> for row in rows:
        if row == None:
           print 'asdf'
        if row == 'None':    ### just to be sure
           print 'jkl'
        if not row == 'None':
          print 'not asdf'
        if not row == None:
          print 'not jkl'

And nothing is printed.   Nothing at all.

What's the deal.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
You need to copy and paste the exact indentation you are using, that is probably where your error is.
0 Kudos
kyleturner
New Contributor III
All:

To help expedite this matter, I attached a very brief snippet of my code along with the necessary table (the table is zipped up because I can't attach a dbf).
This is really starting to make me crazy.

Chris: Your suggestion looks like it's the ticket, but can you 'dumb it down' a bit.

import arcgisscripting
gp = arcgisscripting.create(9.3)
table = 'C:/Users/lilaandmax/Downloads/AttributeCrosswalk.dbf'
####### the following three0FC works 
three0FC = 'AirfieldSurface'
######## but if you do this, three0FC = 'XAirfieldSurface'  ## nothing happens 
query = "Three0FCla = '" + three0FC + "'"
row, rows = None, None
rows = gp.SearchCursor(table, query)

for row in rows:
    if row.isNull("Three0FCla"):
        print "isNull"
    elif row == None:
        print "None"
    elif row == "":
        print "emptyspace"
    else:
        print row.getValue("Three0FCla")



Thanks
0 Kudos
MathewCoyle
Frequent Contributor
If your cursor is empty, it will never step in. Nothing in the loop
for row in rows:

will execute because there is no row in rows, if you follow.

So I tested your code and came back with 4 rows, is that right? i am confused, I thought you wanted to test if the cursor object was empty?
0 Kudos
ChrisSnyder
Regular Contributor III
Sure - see (code comments denoted by the # symbol):

#STEP1: Execute a SQL query (the MakeFeatureLayer tools is probably the quickest way to do this)
arcpy.MakeFeatureLayer_management(indxTileFC, indxTileFL, "HCPUNIT_NM <> 'OESF')
#STEP2: Are any records returned? If 0 records satisfy the SQL query, then let us know! If 0 records are returned, the SQl query is probably incorrect!!!
if int(arcpy.GetCount_management(indxTileFL).getoutput(0)) == 0:
   print "Query returned no records!"
#STEP3: If there are records returned by the query, then run a search cursor and look for Null field values in MY_FIELD
else:
   #Create a search cursor object using the same query as the one in the MakeFeatureLayer 
   searchRows = arcpy.SearchCursor(indxTileFC, "HCPUNIT_NM <> 'OESF')
   #For each row in the collection of search cursor rows
   for searchRow in searchRows:
      #if the value in the "MY_FIELD" field is Null let us know... If theer are no Null values in the MY_FIELD field, then no message will be printed.
      if searchRow.MY_FIELD == None: #Another way: if searchRow.isNull("MY_FIELD") == True:
         print "MY _FIELD is Null!"
   #Delete the search cursor objects so the read lock is released on the search cursor table
   del searchRow, searchRows
0 Kudos
ChrisSnyder
Regular Contributor III
Yes, like Mathew says, your issue is that the SQL statement you provide in your cursor is probably incorrect and is returning 0 records (which is why you get no cursor row object). My code will only run the cursor if your query returns 1 or more records.
0 Kudos
kyleturner
New Contributor III
If your cursor is empty, it will never step in. Nothing in the loop
for row in rows:

will execute because there is no row in rows, if you follow.

So I tested your code and came back with 4 rows, is that right? i am confused, I thought you wanted to test if the cursor object was empty?


Duh...I should have tested for row == None outside the for/loop, being sure to set row = None before the call to Searchcursor....duh.
Yes, the code as shown will find something, because "AirfieldSurface" is in the dbf. However, change the FCls30 variable to something like, ZZZ, and see what happens.

Thanks again.
0 Kudos