Select to view content in your preferred language

Using InsertCursor method with DATE type field.

5669
2
07-04-2010 12:16 PM
BorisBorisov
New Contributor
How can I use the InsertCursor method with DATE type field? I'm trying to use this code, but have an error (progresscheck - table in my gdb, date - field with date data type):

...
rows = gp.InsertCursor(progresscheck)
row = rows.NewRow()

row.date = "22.10.2010"
...

or

...
rows = gp.InsertCursor(progresscheck)
row = rows.NewRow()

row.date = time.strftime("%d.%m.%Y",time.localtime(time.time()))
...

Error message:
Traceback (most recent call last):
File "C:\work\progress\1\1.py", line 61, in <module>
row.date = time.strftime("%d.%m.%Y",time.localtime(time.time()))
RuntimeError: ERROR 999999: Error executing function.
0 Kudos
2 Replies
KimOllivier
Honored Contributor
An interesting problem that is glossed over in the help.
I solved this by creating a table with a date field, populating it and then reading them with a SearchCursor.

When Python reads/writes a date field using a cursor it expects a date object using the datetime module. So I found that you have to create and manipulate dates as datetime objects.

The datetime module is actually very convenient and cursors work for everything except NULL values.
# datetest

import arcpy,datetime

ws = "c:/home/python/datetest.gdb"
arcpy.env.workspace = ws
rows = arcpy.UpdateCursor("fcdate")
for row in rows :
    print row.timestamp,type(row.timestamp)
    if row.timestamp == None:
        row.timestamp = datetime.datetime.now()
    else :
        row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15)
    rows.updateRow(row)
    
del row,rows    


Here is an example featureclass with an existing date field. It had a few dates and a null record.
Note that you can read the dates and print them because a date object has a default 'representation'. It is not a string, you can just print the object as a string without casting.
Note how you increment a date, using a timedelta object.
You also have to trap null dates.
I have used a file geodatabase (not a shapefile) because I expected it to handle a full datetime not just a date as in dBase or INFO.

More help in the pythonwin help for python for the datetime module.
0 Kudos
KimOllivier
Honored Contributor
How hard could it be??

Unfortunately this example script is read-only at 9.3. You will have to upgrade to arcpy to insert dates as I have found to my cost.

1/08/2010 <type 'unicode'>
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\eudora\Attachdir\datetest93.py", line 16, in <module>
    row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15)
TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found

# datetest93.py
# convert to 9.3 after a problem
import arcgisscripting,datetime

gp = arcgisscripting.create(9.3)
ws = "d:/workspace/datetest.gdb"
gp.workspace = ws
rows = gp.UpdateCursor("fcdate")
row = rows.next()
while row :
    print row.timestamp,type(row.timestamp)
    if row.timestamp == None:
        row.timestamp = datetime.datetime.now() ## fails 
        pass
    else :
        row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15) ## fails
        pass
    rows.updateRow(row)
    row = rows.next()
del row,rows
0 Kudos