in Arc10 using arcgisscripting.create(9.3) how do I write a cursor to work in 9.3?

2640
4
06-26-2012 05:42 AM
kyleturner
New Contributor III
With help from the forum, I thought I figured out how to write a cursor to work in 9.3 and 10 but users report not.

Here's my code:
 

import arcgisscripting, sys, os, string, traceback
gp = arcgisscripting.create(9.3)
# Set to write over previous outputs if they exist
gp.overwriteoutput = False
def which30(two6ftrcls):

        two6FC = two6ftrcls
        whereclause = "Two6FCs = '" + two6FC + "'"
        gp.AddMessage("whereclause is " +whereclause)
        crosswalk = ShellPath + "/Two630FC_IDPKxwalk.dbf"
        # The variables row and rows are initially set to None, so that they
        #  can be deleted in the finally block regardless of where (or if)
        #  script fails.
        row, rows = None, None
        Three0ftrlst = []
        FC_26namelst = []
        row, rows = None, None
        Three0ftrlst = []
        FC_26namelst = []
        rows = gp.SearchCursor(crosswalk,whereclause)
        row = rows.next()
        if (row == None):
            gp.AddMessage("check the Two630FC_IDPKxwalk.dbf table for Two6FCs -->" +two6FC)
        elif (row != None):
            while row:
                if not row.isNull("Three0FCs"):
                  Three0ftr=row.getValue("Three0FCs")
                  gp.AddMessage("Three0ftr is " +Three0ftr)
                  Three0ftrlst.append(str(Three0ftr))
                  FC_26name = row.getValue("FC_26")
                  FC_26namelst.append(str(FC_26name))
               row = rows.next()

        if row: del row
        if rows: del rows

        return Three0ftrlst, FC_26namelst



Works in 10 but not in 9.3 (so I'm told).

Attached is a screenshot of the error message.
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
The easier way to go about this would be to write two functions. One for 10.0 and one for 9.3 and test the version when the script executes to determine which function to execute.
0 Kudos
kyleturner
New Contributor III
Matthew,

Thanks.
Inititially I thought:

row=rows.next()
while row:
    code....

    row=rows.next()


would work regardless of the version, but it doesn't seem so.
The confusing thing is that when I run it in my XP VM machine using 9.3, it works fine for me, but other users report that it's not working for them...??

So, I can't confirm that this is infact the problem with my code, but the error message seems to imply that it is.
I'm starting to think maybe there is something wrong with my ftrClsList = gp.ListFeatureClasses(), but why does it work for me???

I'm at my wits end...
Any more ideas is greatly appreciated.

Cheers
0 Kudos
RDHarles
Occasional Contributor
How about this version of the script without that "next() stuff":

import arcgisscripting, sys, os, string, traceback
gp = arcgisscripting.create(9.3)
# Set to write over previous outputs if they exist
gp.overwriteoutput = False
def which30(two6ftrcls):

        two6FC = two6ftrcls
        whereclause = "Two6FCs = '" + two6FC + "'"
        gp.AddMessage("whereclause is " +whereclause)
        crosswalk = ShellPath + "/Two630FC_IDPKxwalk.dbf"
        # The variables row and rows are initially set to None, so that they
        #  can be deleted in the finally block regardless of where (or if)
        #  script fails.
        row, rows = None, None
        Three0ftrlst = []
        FC_26namelst = []
        row, rows = None, None
        Three0ftrlst = []
        FC_26namelst = []
        rows = gp.SearchCursor(crosswalk,whereclause)        
        if (row == None):
            gp.AddMessage("check the Two630FC_IDPKxwalk.dbf table for Two6FCs -->" +two6FC)
        elif (row != None):
            for row in rows:
                if not row.isNull("Three0FCs"):
                  Three0ftr=row.getValue("Three0FCs")
                  gp.AddMessage("Three0ftr is " +Three0ftr)
                  Three0ftrlst.append(str(Three0ftr))
                  FC_26name = row.getValue("FC_26")
                  FC_26namelst.append(str(FC_26name))               

        if row: del row
        if rows: del rows

        return Three0ftrlst, FC_26namelst
0 Kudos
curtvprice
MVP Esteemed Contributor
With help from the forum, I thought I figured out how to write a cursor to work in 9.3 and 10 but users report not.


Here's how I do it. This should run at 9.x and 10.x.

Note the use of a logical expression to check for None (definitely works in this context) and, a try/finally block to make sure the cursor objects get deleted.

import arcgisscripting
gp = arcgisscripting.create(9.3)
try:
    rows = gp.SearchCursor(crosswalk,whereclause)
    row = rows.next()
    if not row:
         gp.AddError("check the Two630FC_IDPKxwalk.dbf table for Two6FCs -->" +two6FC)
         raise
    while row:
        # do row stuff...
        row = rows.next()
except:
    raise
finally:
    del row, rows


The 10.0 arcpy syntax is easier, as search cursors return an iterable:

rows = arcpy.SearchCursor(crosswalk,whereclause)
for row in rows:
    # do row stuff
0 Kudos