I need help on the Update Cursor function

7018
42
Jump to solution
08-23-2012 07:27 AM
OLANIYANOLAKUNLE
Frequent Contributor
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
42 Replies
MathewCoyle
Honored Contributor
As Chris mentioned earlier, you need to have setValue.

rows = arcpy.UpdateCursor("Parcels", "FID = 1")

for row in rows:
    row.setValue("GENERATED", "GENERATED")
    rows.updateRow(row)
0 Kudos
ChrisSnyder
Honored Contributor
but can i start an edit session automatically with arcpy script


Actually, in v10.1, yes you can:
http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000005000000

Although I feel that extra complexity might not be what you actually want to do in this case...
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
mzcoyle, please there is no difference between the code you pasted and the code i used? Please can you elaborate on the setvalue? Thanks
0 Kudos
ChrisSnyder
Honored Contributor
setvalue vs setValue

Python is case sensitive... Meaning there is a difference between upper case (V) and lower case (v).
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
I used your code

Code:
rows = arcpy.UpdateCursor("Parcels", "FID = 1")

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

i did not get any error but the record was not updated? Any suggestions
0 Kudos
ChrisSnyder
Honored Contributor
Only use setValue() if the field name is a variable.

if your field IS NOT a variable:
rows = arcpy.UpdateCursor("Parcels", "FID = 1")
for row in rows:
    row.GENERATED = "GENERATED"
    rows.updateRow(row)
if your field IS a variable:
myField = "GENERATED"
rows = arcpy.UpdateCursor("Parcels", "FID = 1")
for row in rows:
    row.setValue(myField,"GENERATED")
    rows.updateRow(row)
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
i want to generate a pdf output by using the arcpy.exporttopdf function, i want the final output to use this nomenclature for naming the pdf output (plot_no.pdf); the plot_no would be derived from an attribute value under the field named plot_no in the attribute table. Please do you have any suggestions. Thanks
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks all i got it with all your suggestions, what happened was that i had to start an edit session to be able to see the changes. With this i have some layers from a parcel fabric within my dataframe which makes it difficult for me to use the edit = arcpy.da.Editor(workspace) it throws this error - can not open workspace. Any suggestion would go a long way. Thanks
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Im trying to automatically get the system time and date updated with the update cursor script, this is my script and the error


myField = "Date_Gener"
>>> rows = arcpy.UpdateCursor("Parcels")
>>> for row in rows:
...    row.setValue(myField,'Date (  )')
...    rows.updateRow(row)
...   
Runtime error
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 1007, in setValue
    return convertArcObjectToPythonObject(self._arc_object.SetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.
The value type is incompatible with the field type. [Date_Gener]

Any suggestions? Please....
0 Kudos
curtvprice
MVP Alum

myField = "Date_Gener"
>>> rows = arcpy.UpdateCursor("Parcels")
>>> for row in rows:
...    row.setValue(myField,'Date (  )')
...    rows.updateRow(row)
...   


Why not get the date as a string and use the Calculate Field tool? This is easier and much faster than a cursor, even the newfangled arcpy.da flavor.

import datetime
Today = str(datetime.date.today())
arcpy.CalculateField_management("Parcels","date_Gener","'" + Today + "'","PYTHON")


This will only calculate the value for selected rows of the layer "Parcels".
0 Kudos