I need help on the Update Cursor function

4974
42
Jump to solution
08-23-2012 07:27 AM
OLANIYANOLAKUNLE
Occasional Contributor II
Im trying to update a field called "GENERATED" with an attribute called "GENERATED" every time i autogenerate a map, i constructed this script and it is not updating and i didnt see an error(s)

rows = arcpy.UpdateCursor("Parcels",'[OBJECTID] = "1"')
row = rows.next()
while row:
    row.setvalue(GENERATED, GENERATED)
    rows.updateRow(row)
    row = rows.next()

Please any suggestions. Im desperate. Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChristopherThompson
Occasional Contributor III
I suspect the naming is throwing it off, if GENERATED is the field name, it looks like you're trying to set the value equal to itself.  Perhaps this will work:
row.setvalue(GENERATED, 'GENERATED')
That way the function can distinguish between a field and a value.
I've also had success with:
row.GENERATED = 'GENERATED'

View solution in original post

0 Kudos
42 Replies
ChristopherThompson
Occasional Contributor III
I suspect the naming is throwing it off, if GENERATED is the field name, it looks like you're trying to set the value equal to itself.  Perhaps this will work:
row.setvalue(GENERATED, 'GENERATED')
That way the function can distinguish between a field and a value.
I've also had success with:
row.GENERATED = 'GENERATED'
0 Kudos
MathewCoyle
Frequent Contributor
Try this.


rows = arcpy.UpdateCursor("Parcels", "[OBJECTID] = 1")

for row in rows:
    row.setValue("GENERATED", "GENERATED")
    rows.updateRow(row)


You shouldn't have quotes around your objectid query selection. Also not sure if you have assigned GENERATED as a variable, but if you haven't you will need quotes. Also make sure you are using the right field delimiters for your OBJECTID field.
0 Kudos
markdenil
Occasional Contributor III
First off, with an arcpy cursor you don't use .next() in a while loop,
you simply itterate the cursor:
for row in rows:
    ...do stuff


Your query string looks problematic
'[OBJECTID] = "1"'
should be something like
'"OBJECTID" = \'1\''
using double quotes on the field instead of bracket delimiters and
single quotes on the string (or no quotes it it is not a string)
The single quotes around the string have to be escaped with a backslash so
you can wrap the whole query in single quotes.
OR you could wrap it all in tripple quotes and not escape....

...three simultaneous answers. that must get some sort of prize.
0 Kudos
MathewCoyle
Frequent Contributor

...three simultaneous answers. that must get some sort of prize.


The trifecta crown.
0 Kudos
ChrisSnyder
Regular Contributor III
I might add that your original sytax of .setvalue should in fact be .setValue (case now matters in v10.0 +!!!). also, I think your SQL in the cursor statement was incorrect. Note you ONLY need to use the .setValue() method if your field name is a variable.

This should work:

rows = arcpy.UpdateCursor("Parcels", "OBJECTID = 1")
for row in rows:
    row.GENERATED = 'GENERATED'
    rows.updateRow(row)
del row, rows


Alternately, this should also work, and demonstates the proper use of the .setValue() method:
generatedFieldName = "GENERATED"
rows = arcpy.UpdateCursor("Parcels", "OBJECTID = 1")
for row in rows:
    row.setValue(generatedFieldName) = 'GENERATED'
    rows.updateRow(row)
del row, rows
0 Kudos
ChrisSnyder
Regular Contributor III
For SAG's (American slang), here's the v10.1 equivalent using the new "data access" update cursor syntax:

fieldList = ["GENERATED"] #list of the field names you want the cursor to return... order is important
rows = arcpy.da.UpdateCursor("Parcels", fieldList, "OBJECTID = 1")
for row in rows:
    row[0] = 'GENERATED' #fields are now refered to in terms of their index in the fieldList
    rows.updateRow(row)
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks for all your response, but can i start an edit session automatically with arcpy script? Thanks
0 Kudos
MathewCoyle
Frequent Contributor
Thanks for all your response, but can i start an edit session automatically with arcpy script? Thanks


No, that would require ArcObjects.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
This is the new code i constructed:


rows = arcpy.UpdateCursor("Parcels", "FID = 0")
>>> for row in rows:
...     row.setvalue("GENERATED", "GENERATED")
...     rows.updateRow(row)
...    

and this is the error message i received:

Runtime error
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__
    raise AttributeError("%s" % attr)
AttributeError: setvalue
0 Kudos