2

I created a table with three fields but there are no values in these fields. Here is the screenshot of these fields.

enter image description here

I want to add a new row with zeros for all these three fields. Here is my code.

with arcpy.da.UpdateCursor(selectionname,("OBJECTID","FREQUENCY","SUM_Shape_Area")) as cursor:
for row in cursor:
   if row[0] == None:
      row[0] = 0
      cursor.updateRow(row)
   else:
      print selectionname + " is not empty"

Selectionname is my table, a parameter defined prior to this part of my code. First, I need to check if these three fields are empty, then adding a new row with zeros if they are empty. After several tries, I still could not add a new row to the table.

1
  • 4
    To add a new row you need to be using an InsertCursor.
    – PolyGeo
    Commented Apr 11, 2019 at 2:46

1 Answer 1

5

Use GetCount to check if table is empty and da.InsertCursor to insert a row. I dont think you should try to set objectid, let it be set automatically. If you want your own ID then add another field for it.

import arcpy
table = 'C:\data.gdb\table123' #Change
fields = ['freq','sum'] #Change

if int(arcpy.GetCount_management(table).getOutput(0)) == 0:
    icur = arcpy.da.InsertCursor(table,fields)
    icur.insertRow([0,0])
    del icur
1
  • 1
    this is what I want and really appreciate your help! @BERA
    – Zhenyu
    Commented Apr 11, 2019 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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