Problem with where clause and Update Cursor

6009
9
Jump to solution
09-20-2013 01:24 PM
DavidDenham
Occasional Contributor
I am working on a script that will allow users to select a project from a pull down list in the tool and then update the status.  I am using an update cursor with a where clause to update the attributes for all of the polygons with the same name.  When I try to run the script I get an error message that the where clause is not a string. Here is the code the where clause is in bold:

# Import arcpy and date time module import arcpy import datetime   # Local variables: today = datetime.date.today() mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0] parcels = arcpy.mapping.ListLayers(mxd, "Parcels", df) [0]   # Geting user input for project name, status, type, submital date and planner name ProjectName = arcpy.GetParameterAsText(0) ProjStatus = arcpy.GetParameterAsText(2) SubmitDate = arcpy.GetParameterAsText(1) DevelopeType = arcpy.GetParameterAsText(3) PlannerName = arcpy.GetParameterAsText(4)   PlanningProjectTracking = "C:\\David Projects\\Planning Project Tracking\\PythonProjectTracking.mdb\\PlanningProjectTracking"   whereClause = "[ProjectName] = '{0}'".format(ProjectName)   #Used update cursor to create update project tracking records. Ucursor = arcpy.da.UpdateCursor(PlanningProjectTracking, whereClause, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"]) for row in Ucursor:    row.setValue (ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today)    Ucursor.updateRow(row) arcpy.RefreshActiveView()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
You've mixed up the argument order, the list of fields goes before the where clause. In addition, you're mixing up your cursor models. There's no row.setValue in da.UpdateCursor.

Ucursor = arcpy.da.UpdateCursor(PlanningProjectTracking, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"], whereClause) for row in Ucursor:    Ucursor.updateRow([ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today])

View solution in original post

0 Kudos
9 Replies
JasonScheirer
Occasional Contributor III
You've mixed up the argument order, the list of fields goes before the where clause. In addition, you're mixing up your cursor models. There's no row.setValue in da.UpdateCursor.

Ucursor = arcpy.da.UpdateCursor(PlanningProjectTracking, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"], whereClause) for row in Ucursor:    Ucursor.updateRow([ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today])
0 Kudos
DavidDenham
Occasional Contributor
You've mixed up the argument order, the list of fields goes before the where clause. In addition, you're mixing up your cursor models. There's no row.setValue in da.UpdateCursor.

Ucursor = arcpy.da.UpdateCursor(PlanningProjectTracking, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"], whereClause)
for row in Ucursor:
   Ucursor.updateRow([ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today])


Thanks for your suggestion, but I checked the update cursor page on the 10.1 resource center and the syntax that they show is UpdateCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_field}).  I tried to use what you suggested, but it still failed to run with an error message TypeError: updateRow() takes exactly 1 argument (6 given).  I tried to include "",  to hold the place for the spatial reference, but that did not work either.  I will have to see if I can select the features first and then update just the selected features.  This is not the way that I would like to do it since I would like to keep my code as simple as possible.  I
0 Kudos
JasonScheirer
Occasional Contributor III
arcpy.da.UpdateCursor and arcpy.UpdateCursor are two different things, your script looks like it's creating an arcpy.da.UpdateCursor but treating it like an arcpy.UpdateCursor. You may be able to get away with your original script just by doing this:

Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])
0 Kudos
DavidDenham
Occasional Contributor
arcpy.da.UpdateCursor and arcpy.UpdateCursor are two different things, your script looks like it's creating an arcpy.da.UpdateCursor but treating it like an arcpy.UpdateCursor. You may be able to get away with your original script just by doing this:

Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])


Thanks for the help, but now I am getting an error message AttributeError: Object: Error in parsing arguments for UpdateCurso.  Here is the code that I changed since the last time I posted:
Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, "", ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])for row in Ucursor:
   row.setValue (ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today)
   Ucursor.updateRow(row)
arcpy.RefreshActiveView()
0 Kudos
JasonScheirer
Occasional Contributor III
Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, "", ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])for row in Ucursor:
   for fieldname, value in [('PROJECTNAME', ProjectName), ('STATUS', ProjStatus), ('SUBMITTALDATE', SubmitDate), ('DEVELOPEMENTTYPE', DevelopeType), ('PLANNER', PlannerName), ('EDITDATE', today)]:
       row.setValue(fieldname, value)
   Ucursor.updateRow(row)
0 Kudos
DavidDenham
Occasional Contributor
Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, "", ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])for row in Ucursor:
   for fieldname, value in [('PROJECTNAME', ProjectName), ('STATUS', ProjStatus), ('SUBMITTALDATE', SubmitDate), ('DEVELOPEMENTTYPE', DevelopeType), ('PLANNER', PlannerName), ('EDITDATE', today)]:
       row.setValue(fieldname, value)
   Ucursor.updateRow(row)


I tried that out and I received a syntax error with:
Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, "", ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])for row in Ucursor:


I thought that the for row in Ucursor: was supposed to be on the next line, but when I tried that I still had an error:

Traceback (most recent call last):
  File "C:\David Projects\Planning Project Tracking\PythonUpdateProjectTracking.py", line 37, in <module>
    Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause, "", ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"])
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\__init__.py", line 1165, in UpdateCursor
    return gp.updateCursor(dataset, where_clause, spatial_reference, fields, sort_fields)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 364, in updateCursor
    self._gp.UpdateCursor(*gp_fixargs(args, True)))
AttributeError: Object: Error in parsing arguments for UpdateCursor


Failed to execute (UpdateProjectTracking).

I don't mind trying to use the selecting by attribute first, but I hate not knowing why this is not working.
0 Kudos
JasonScheirer
Occasional Contributor III
The initial arcpy.da solution I provided should have worked if it had been correctly copied. The updateRow() call needs the list of values to be in a [list, like, this].

Either do this:

with arcpy.da.UpdateCursor(PlanningProjectTracking, ["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"], whereClause) as Ucursor:
    for row in Ucursor:
       Ucursor.updateRow([ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today])


or

Ucursor = arcpy.UpdateCursor(PlanningProjectTracking, whereClause)
for row in Ucursor:
   for fieldname, value in (('PROJECTNAME', ProjectName),
                            ('STATUS', ProjStatus),
                            ('SUBMITTALDATE', SubmitDate),
                            ('DEVELOPEMENTTYPE', DevelopeType),
                            ('PLANNER', PlannerName),
                            ('EDITDATE', today)):
       row.setValue(fieldname, value)
   Ucursor.updateRow(row)



You do not need to specify all the arguments in the cursor. If a parameter is marked as optional in the documentation, you can omit it.
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi David,

Not sure if this has anything to do with it, but I notice in your original post that you obtain your "SubmitDate" through:
SubmitDate = arcpy.GetParameterAsText(1)


Your "SubmitDate" is a string. I don't know how you have defined the type of your field "SUBMITTALDATE", but if it is a date field, it may be better to provide an actual date. To convert a string to a datetime object see snippet below (but, you need to know the format how it's specified):

from datetime import datetime

submitDate = '24-9-2013'
dateObj = datetime.strptime(submitDate,'%d-%m-%Y')

submitDate = '9/24/2013'
dateObj = datetime.strptime(submitDate,'%m/%d/%Y')


More info on syntax:
http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

The alternative could be to read it as an object if it's provided as a date:
SubmitDate = arcpy.GetParameter(1)


See also the Help:
http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000038000000


Kind regards,

Xander
0 Kudos
DavidDenham
Occasional Contributor
I am going to blame this on sleep deprivation from staying up with the my kids.  I finally realized what  you said in your first reply and switched from using the old arcpy.UpdateCursor and used the arcpy.da.UpdateCursor instead.  Then I used the parameters in the correct order and the tool worked fine. 

# Import arcpy and date time module
import arcpy
import datetime


# Local variables:
today = datetime.date.today()
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]


ProjectTracking = arcpy.mapping.ListLayers(mxd, "PlanningProjectTracking", df) [0]


# Geting user input for project name, status, type, submital date and planner name
ProjectName = arcpy.GetParameterAsText(0)
ProjStatus = arcpy.GetParameterAsText(2)
SubmitDate = arcpy.GetParameterAsText(1)
DevelopeType = arcpy.GetParameterAsText(3)
PlannerName = arcpy.GetParameterAsText(4)


#Set variable to hold SQL statement to query out project to update
whereClause = "[ProjectName] = '{0}'".format(ProjectName)


#Used update cursor to create update project tracking records.
Ucursor = arcpy.da.UpdateCursor(ProjectTracking,["PROJECTNAME","STATUS","SUBMITTALDATE","DEVELOPEMENTTYPE","PLANNER","EDITDATE"],whereClause)
for row in Ucursor:
    irow = (ProjectName,ProjStatus,SubmitDate,DevelopeType,PlannerName,today)
    Ucursor.updateRow(irow)
arcpy.RefreshActiveView()
0 Kudos