I need help on the Update Cursor function

6758
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
ChristopherThompson
Frequent Contributor
curtvprice;227108 wrote:
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.


I've often wondered about this, even if you have a series of updates or calculations to make, seems you could easily make a list of those, and iterate through that list.  The advantages of a cursor are clear when you have to make decisions about how to handle a particulare feature or row on a case by case basis.  But even for simple mass updates like this, doesn't the Calc Field tool simply implement a cursor behind the scenes?  Why is that inherently faster? Is it because the cursor doesn't have to work through the interpreter?
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks Curtis Price, but please how can i include the current time also. Thanks
0 Kudos
curtvprice
MVP Alum
  how can i include the current time also.


The details are in the Python documentation.

http://docs.python.org/library/time.html

import datetime
now = datetime.datetime.now()
strNow = now.strftime("%d %b %Y %H:%M:%S")
0 Kudos
curtvprice
MVP Alum
doesn't the Calc Field tool simply implement a cursor behind the scenes?  Why is that inherently faster? Is it because the cursor doesn't have to work through the interpreter?


I don't know whether Calculate Field uses cursors, but I do know that there are at least three kinds of cursors in the ArcGIS universe: 1. arcpy cursors, 2. ArcObjects cursors (accessed through .NET) and 3. (new at 10.1) arcpy.da cursors.

Calculate Field can do complex decision making with a code block, but can only write to one field. All I know is that CF is much faster and more convenient to code, so for me it's preferable if you only need to write to one field.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
curtvprice, i intend to include the time in a field called Time_Generated?

import datetime
now = datetime.datetime.now()
strNow = now.strftime("%d %b %Y %H:%M:%S")

Where do i add the field?
0 Kudos
ChrisSnyder
Honored Contributor
Just to debate Curtis' statement about cursors vs. CalculateField...

In my experience (which may be outdated now), when using the "PYTHON*" option in the CalculateField tool, the performace of the CalculateField tool and the gp/arcpy.UpdateCursor are exactly the same. A post on this topic from four years ago: http://forums.esri.com/Thread.asp?c=93&f=1729&t=262236#806786. In my mind, complex conditional expressions are much easier to deal with in an update cursor, and I generally reserve the CalculateField tool for only very simple expressions.

I have found new arcpy.da cursors are significantly faster (for a number of reasons) and are much more akin to the performace and functionality of the ArcObjects cursors and more similar in performance to the "VB" option in the CalculateField tool.
0 Kudos
curtvprice
MVP Alum
Just to debate Curtis' statement about cursors vs. CalculateField...


Thanks Chris - I hope you meant "discuss" -- the "right" way to do something can include many factors, including whether you need to support multiple versions of ArcGIS, what is easiest for you to code/maintain, what you "like", etc....

This is helpful information. I hadn't done any testing and probably didn't notice because like you, I tend to use Calculate Field for simple expressions using VBScript (the default), which as you have said - is faster than python cursors. Great to hear that arcpy.da cursors are near the speed of ArcObjects cursors.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks curtvprice for the nugget of information, please can you help me with this
Please can you kindly help with this script also; i am trying to do a search through my attribute table, for a field called TDP_Status and if the attribute is GENERATED, I want the arcpy script to print TDP ALREADY GENERATED and stop the process else it should continue with the buffer analysis. This is my small script


generatedFieldName = "TDP_Status"
rows = arcpy.SearchCursor("Parcels", '"TDP_Status" = \'GENERATED\'')
for row in rows:
if row.TDP_Status = 'GENERATED'
print TDP ALREADY GENERATED
else:
arcpy.Buffer_analysis ("Parcels", "ClipFeature", "12.5 meters", "FULL", "ROUND", "LIST")

Any suggestions?
0 Kudos
ChrisSnyder
Honored Contributor
Since you are only quering records where  'TDP_Status" = 'GENERATED'' in your cursor, the else statment (the Buffer) will never fire off.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
So what do you suggest i do? Please
0 Kudos