Select to view content in your preferred language

UpdateCursor Script Tool

3129
35
05-12-2010 07:47 AM
Corey_C_Denninger
Deactivated User
I am attempting to write a simple script tool that looks at one field, and if it meets a specified value(s) then populate another field with a String.  I plan to make the script a bit longer and more complex, as of now, the simple version (below) does not work.  Any help is appreciated.  Thank you.

BTW - I beleive my indentations are correct, but the script is not posting/displaying below the way I indented.

import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1

#gp.workspace = "L:\\GIS\Layers\\Geodatabases\\Land_Use_Cover\\Districtwide_LULC_Analysis.mdb"

FC = sys.argv[1]
LEV2 = sys.argv[2]
UpdateField = sys.argv[3]

cur = gp.UpdateCursor(FC)
row = cur.Next()

while row:
            if row.GetValue(LEV2) == 11:
            row.SetValue(UpdateField,"Urban & Built-Up")
            cur.UpdateRow(row)
      row = cur.Next()

del cur
del row
0 Kudos
35 Replies
RDHarles
Regular Contributor
Does your code look something like this (other than I changed the table names and mdb name so I would test here).

This code works:

import arcgisscripting
gp = arcgisscripting.create()

gp.Workspace = "C:/junk/temp.mdb"

inputFC = "A"
TAB = "B"

#Table Loop
cur1 = gp.SearchCursor(TAB)
row1 = cur1.Next()
while row1:
    
    #Get the values from the fields(tab)
    c = row1.GetValue("CATEGORY")
    s = row1.GetValue("SUBNO")
    p = row1.GetValue("PROJECT")    

    print "\nc = "+str(c)
    print "s = "+str(s)
    print "p = "+str(p)

    #fc loop
    cur2 = gp.UpdateCursor(inputFC)
    row2 = cur2.Next()
    while row2:
        #Get the values from the fields(fc)
        t = row2.GetValue("Category")
        l = row2.GetValue("LISTNUM")
        n = row2.GetValue("SUBDIV")

        print "t = "+t
        print "l = "+l
        print "n = "+str(n)

        #Populate field in the fc based on a match
        if t == c and l == s:
            row2.SUBDIV = p            

        #Execute the new value to the fc
        cur2.UpdateRow(row2)
        #fc next
        row2 = cur2.Next()

    #table next
    row1 = cur1.Next()

del cur2
del cur1

print "\nDone. \n" 
0 Kudos
RickyLindley
Deactivated User
Does your code look something like this (other than I changed the table names and mdb name so I would test here).

This code works:

import arcgisscripting
gp = arcgisscripting.create()

gp.Workspace = "C:/junk/temp.mdb"

inputFC = "A"
TAB = "B"

#Table Loop
cur1 = gp.SearchCursor(TAB)
row1 = cur1.Next()
while row1:
    
    #Get the values from the fields(tab)
    c = row1.GetValue("CATEGORY")
    s = row1.GetValue("SUBNO")
    p = row1.GetValue("PROJECT")    

    print "\nc = "+str(c)
    print "s = "+str(s)
    print "p = "+str(p)

    #fc loop
    cur2 = gp.UpdateCursor(inputFC)
    row2 = cur2.Next()
    while row2:
        #Get the values from the fields(fc)
        t = row2.GetValue("Category")
        l = row2.GetValue("LISTNUM")
        n = row2.GetValue("SUBDIV")

        print "t = "+t
        print "l = "+l
        print "n = "+str(n)

        #Populate field in the fc based on a match
        if t == c and l == s:
            row2.SUBDIV = p            

        #Execute the new value to the fc
        cur2.UpdateRow(row2)
        #fc next
        row2 = cur2.Next()

    #table next
    row1 = cur1.Next()

del cur2
del cur1

print "\nDone. \n" 




Here's mine.....

import win32com.client

gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

gp.Workspace = "C:\\GISProjects\\ProjectListUpdate.mdb"

FC = "Lines"
TAB = "PROJECTLIST"

#Table Loop
cur1 = gp.SearchCursor(TAB)
row1 = cur1.Next()
while row1:
    #Get the values from the fields(tab)
    c = row1.GetValue("CATEGORY")
    s = row1.GetValue("SUBNO")
    p = row1.GetValue("PROJECT")

    print "Table Category ="+str(c)
    print "Table Subno ="+str(s)
    print "Table Project Name ="+str(p)

    #fc loop
    cur2 = gp.UpdateCursor(FC)
    row2 = cur2.Next()
    while row2:
        #Get the values from the fields(fc)
        t = row2.GetValue("Category")
        l = row2.GetValue("LISTNUM")
        n = row2.GetValue("SUBDIV")

        #Populate field in the fc based on a match
        if t == c and l == s:
            row2.SUBDIV = p

        #Execute the new value to the fc
        cur2.UpdateRow(row2)
        #fc next
        row2 = cur2.Next()

    #table next
    row1 = cur1.Next()

del cur2
del cur1

print "\nDone. \n"
0 Kudos
RDHarles
Regular Contributor
OK,
1.) copy & paste my code
2.) edit these 3 to match your mdb and file names:
gp.Workspace = "C:/junk/temp.mdb"
InputFC = "A"
TAB = "B"
3.) Let me know what happens.
0 Kudos
RickyLindley
Deactivated User
OK,
1.) copy & paste my code
2.) edit these 3 to match your mdb and file names:
gp.Workspace = "C:/junk/temp.mdb"
InputFC = "A"
TAB = "B"
3.) Let me know what happens.


I added in a few more print commands to see where the code is failing...it looks like the following code is not working for some reason.....maybe it doesn't like using a variable. Like earlier the code runs without any errors but it still doesn't change the name in the SUBDIV field in the FC....I'm stumped.

row2.SUBDIV = p
0 Kudos
RDHarles
Regular Contributor
I added in a few more print commands to see where the code is failing...it looks like the following code is not working for some reason.....maybe it doesn't like using a variable. Like earlier the code runs without any errors but it still doesn't change the name in the SUBDIV field in the FC....I'm stumped.

row2.SUBDIV = p


I know the code works because I've ran it here and it updated the field "SUBDIV".  I have to think it's something simple
like a misspelled field name (in the data or the code) or improper indenting or something like that.
0 Kudos
RickyLindley
Deactivated User
I know the code works because I've ran it here and it updated the field "SUBDIV".  I have to think it's something simple
like a misspelled field name (in the data or the code) or improper indenting or something like that.


I will double-check everything.
0 Kudos
RickyLindley
Deactivated User
I know the code works because I've ran it here and it updated the field "SUBDIV".  I have to think it's something simple
like a misspelled field name (in the data or the code) or improper indenting or something like that.




******
Could there be some kind of lock or permission on the FC in the personal geodatabase that is keeping the field SUBDIV from being updated?
0 Kudos
RDHarles
Regular Contributor
******
Could there be some kind of lock or permission on the FC in the personal geodatabase that is keeping the field SUBDIV from being updated?


I'm not real familiar with personal geodatabase (mdb's).  I created one just to test the code but I don't use them otherwise.  Perhaps you could convert your two tables to a File Geodatabase to test your theory.
0 Kudos
deleted-user-rQoEFM5qzbHE
Deactivated User
Since you were able to understand and help in this thread, I thought I would post my question here. The section of code that is failing is:

rows = gp.SearchCursor("PS_lyr")
row = rows.next()

try:
    while row:
        for i in range(0,numSchools):
            school = row.GetValue(Elem)
            M[1] = school

        M.sort(lambda x, y: cmp(x[1], y[1]))
        M.reverse()

        if M[0][1] > 0:
            school = M[0][0]
            row.SetValue("[ScenarioTemplate_1011." + String + "]", school)

        gp.AddMessage("You made it through the if statement")
        rows.UpdateRow(row)
        row = rows.next()

except:
    gp.AddError("Failed to set the value of new scenario field to new school assignment")


Specifically, it is failing on the row.SetValue line. Any thoughts?

Thanks.
0 Kudos
ChrisSnyder
Honored Contributor
How about getting rid of the square brackets, since you are referencing a field name and not actual SQL? I'm assuming this is a PGDB data source that has some other table joined to it. Note that the field name that the 'String' variable represents must be in the "parent" join table, and not the look up table.

row.SetValue("ScenarioTemplate_1011." + String, school)
0 Kudos