Select to view content in your preferred language

UpdateCursor Script Tool

3115
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
RickyLindley
Deactivated User
rdharles, thanks for the quick post. I am a novice at this and it's been a few years since I have taken the intro class so it might take me a while to incorporate my information into the code to test. This should give me a really good start.

Thanks Again

Below is my last attempt before I gave up last time.

import win32com.client

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

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

inputFC = "Lines"
TAB = "PROJECTLIST"

cur1 = gp.SearchCursor(TAB)

row1 = cur1.Next()

c = row1.GetValue("CATEGORY")
s = row1.GetValue("SUBNO")
p = row1.GetValue("PROJECT")

print c
print s
print p

cur2 = gp.SearchCursor(inputFC)
row2 = cur2.Next()

t = row2.GetValue("Category")
l = row2.GetValue("LISTNUM")
n = row2.GetValue("SUBDIV")
field = "SUBDIV"


print t
print l
print n

print c
print s
print p




if "Category" == c and "LISTNUM" == s:
    cur = gp.UpdateCursor(inputFC)
    row2 = cur2.Next()
while row2:
    row2.SetValue(field, row2.SUBDIV, row1.PROJECT)
    cur2.UpdateRow(row2)
    row2 = cur2.Next()
else:
    row1 = cur1.Next()
   
  
  
     
del cur
0 Kudos
RickyLindley
Deactivated User
rdharles, still not working...any ideas? Thanks.

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.SearchCursor(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
What sort of error are you getting?
0 Kudos
RickyLindley
Deactivated User
What sort of error are you getting?


>>> Table Category =D
Table Subno =5865
Table Project Name =McDermott Road, East from Coit Road.
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Student\MyFolder\test1.py", line 37, in <module>
    cur2.UpdateRow(row2)
  File "<COMObject <unknown>>", line 2, in UpdateRow
com_error: (-2147352567, 'Exception occurred.', (0, u'DAO.TableDefs', u'Item not found in this collection.', u'jeterr40.chm', 5003265, -2147467259), None)
>>>
0 Kudos
RDHarles
Regular Contributor
I think I see it, the cursor on your FC needs to be a UpdateCursor (not a SearchCursor).
0 Kudos
RickyLindley
Deactivated User
I think I see it, the cursor on your FC needs to be a UpdateCursor (not a SearchCursor).


That seems to be it...thanks . I will run it on some test data tomorrow.

Thanks  Again.
0 Kudos
RickyLindley
Deactivated User
I think I see it, the cursor on your FC needs to be a UpdateCursor (not a SearchCursor).


Tested this morning the code runs without any errors....however it doesn't replace the SUBDIV in the FC with the data from the table.

#Populate field in the fc based on a match
        if t == c and l == s:
            print "Match"
            row2.SUBDIV = p
           
        #Execute the new value to the fc
        cur2.UpdateRow(row2)
        #fc next
        row2 = cur2.Next()
0 Kudos
RickyLindley
Deactivated User
Tested this morning the code runs without any errors....however it doesn't replace the SUBDIV in the FC with the data from the table.

#Populate field in the fc based on a match
        if t == c and l == s:
            print "Match"
            row2.SUBDIV = p
           
        #Execute the new value to the fc
        cur2.UpdateRow(row2)
        #fc next
        row2 = cur2.Next()


Any ideas on why the new SUBDIV data does not show in the FC?
0 Kudos
RDHarles
Regular Contributor
Any ideas on why the new SUBDIV data does not show in the FC?


Is it printing the word "Match" and not writing anything or is it not getting into the if statement at all?
0 Kudos
RickyLindley
Deactivated User
Is it printing the word "Match" and not writing anything or is it not getting into the if statement at all?


It looks like it is this part of the code that is not working for some reason. It's not updating the FC field called SUBDIV with the information that is in variable p.
When I open the FC it still has the old info in it. Thanks.

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