2

How to set NULL value by arcpy.da.InsertCursor on date column?

I try insert "NULL", None, '', 0

dictIN= [{'field1':"test1",'date1':'01-02-2015','date2':''},\
 {'field1':"test2",'date1':'01-02-2015','date2':'01-02-2015'},\
 {'field1':"test3",'date1':'','date2':''},\
 {'field1':"test4",'date1':'01-02-2015','date2':''}]


def setNULLIf(field):
    if field == '':
        return None #try NULL,'',0
    else: return field

cursor = arcpy.da.InsertCursor(GDB,("field1", "date1", "date2"))
for x in dictIN:
    cursor.insertRow((x['field1'], setNULLIf(x['date1']), setNULLIf(x['date2'])))

Is it possible by arcpy.da ? I use ArcGIS 10.2. I try insert data to GDB file.

  • What code did you use to try and do this? Please edit your question to include a code snippet. I also recommend that you copy the information about using a date field, that currently only appears in the title where it is easily overlooked, down into the question body. – PolyGeo Feb 27 '15 at 9:13
  • 2
    Why don't you just omit the date field in the InsertCursor? Only the fields specified in the cursor will be filled in. – GISGe Feb 27 '15 at 9:22
  • Please edit the question to specify 1) the version of ArcGIS, and 2) the cursor target data source type (FGDB, shapefile, EGDB,...) – Vince Feb 27 '15 at 17:07
  • You are missing a quotation before field1 , cursor.insertRow((x[field1'], which must be cursor.insertRow((x['field1'] – Farid Cheraghi Mar 3 '15 at 8:52
8
+50

You are missing date2 in dictIN:

Change it:

dictIN= [{'field1':"test1",'date1':'01-02-2015','date2':''},\
 {'field1':"test2",'date1':'01-02-2015','date2':'01-02-2015'},\
 {'field1':"test3",'date1':'','date2':''},\
 {'field1':"test4",'date1':'01-02-2015','date2':''}]

Full code is:

dictIN= [{'field1':"test1",'date1':'01-02-2015','date2':''},\
 {'field1':"test2",'date1':'01-02-2015','date2':'01-02-2015'},\
 {'field1':"test3",'date1':'','date2':''},\
 {'field1':"test4",'date1':'01-02-2015','date2':''}]


def setNULLIf(field):
    if field == '':
        return None #try NULL,'',0
    else: return field

cursor = arcpy.da.InsertCursor(GDB,("field1", "date1", "date2"))
for x in dictIN:
    print (x['field1'], setNULLIf(x['date1']), setNULLIf(x['date2']))

    cursor.insertRow((x['field1'], setNULLIf(x['date1']), setNULLIf(x['date2'])))

Full code returns this to a new feature class in GDB with you fileds: enter image description here

If code is not working try to check GDB field parameters - are they nullable?

| improve this answer | |
2

A simple test shows that None should work. See Mr. Che's answer to see what else was wrong.

with arcpy.da.InsertCursor(mytable, ["date"]) as cursor:
    cursor.insertRow([None])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.