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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 11, 2019 at 2:41
1
  • 4
    To add a new row you need to be using an InsertCursor. 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
answered Apr 11, 2019 at 5:42
1
  • 1
    this is what I want and really appreciate your help! @BERA Commented Apr 11, 2019 at 19:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.