0

In my code I am creating a table with some fields. The fields should be filled with 0 (Datatype of the fields are LONG or DOUBLE). I want to fill them because afterwards I want to update the values. But without a row with default values, I can't update the values.

    tb_path = r'C:\Users\a48384\Desktop\00_ArcGis\Daten'
    tb_name = '\netzplan'
    arcpy.CreateTable_management(tb_path,tb_name)
    tbNetzplan = r'c:\users\a48384\desktop\00_arcgis\daten\netzplan'

    #names of the fields which will be added
    fn_LEN_NS_FREI = "LEN_NS_FREI"
    fn_NS_VERKAB_GRAD = "NS_VERKAB_GRAD"
    fn_LEN_NS_KAB = "LEN_NS_KAB"
    fn_ANZ_NS_KVS = "ANZ_NS_KVS"
    fn_ANZ_NS_KVS_BS = "ANZ_NS_KVS_BS"
    fn_ANZ_NS_KVS_NBS = "ANZ_NS_KVS_NBS"
    fn_ANZ_NS_KVS_NULL = "ANZ_NS_KVS_NULL"

    #Adding fields to the table
    arcpy.AddField_management(tbNetzplan, fn_LEN_NS_FREI, "Double")
    arcpy.AddField_management(tbNetzplan, fn_NS_VERKAB_GRAD, "INTEGER")
    arcpy.AddField_management(tbNetzplan, fn_LEN_NS_KAB, "Double")
    arcpy.AddField_management(tbNetzplan, fn_ANZ_NS_KVS, "INTEGER")
    arcpy.AddField_management(tbNetzplan, fn_ANZ_NS_KVS_BS, "INTEGER")
    arcpy.AddField_management(tbNetzplan, fn_ANZ_NS_KVS_NBS, "INTEGER")
    arcpy.AddField_management(tbNetzplan, fn_ANZ_NS_KVS_NULL, "INTEGER")

Now I would like to add a row with default values. But I don't know how... Afterwards the values are going to be updated:

    cur = arcpy.UpdateCursor(tbNetzplan)
    for row in cur:

        row.insertValue(fn_LEN_NS_FREI, VALUE1)
        row.setValue(fn_NS_VERKAB_GRAD, VALUE2))
        row.setValue(fn_LEN_NS_KAB, VALUE3)
        row.setValue(fn_ANZ_NS_KVS, VALUE4)
        row.setValue(fn_ANZ_NS_KVS_BS, VALUE5)
        row.setValue(fn_ANZ_NS_KVS_NBS, VALUE6)
        row.setValue(fn_ANZ_NS_KVS_NULL, VALUE7)
        cur.updateRow(row)
2
  • 2
    First of all, this is the proper task of a NULL value, which is the default in storage formats which support Nulls (like file geodatabase). Second, you shouldn't be using old-style cursors for any new code. They are slow and non-Pythonic. If you use a FGDB table then use arcpy.da.InsertCursor with a column list that only includes your required columns, your work is done.
    – Vince
    Commented Feb 13, 2019 at 10:38
  • Hi, I haven´t got much time so i will only give some tips. First, you should use the data access module, if you want to use cursors. Tthey are much faster then the standard ones. They are implemented with version 10.3, if i´m correct. So you use: arcpy.da.UpdateCursor(). For new rows i would look into the insert cursor: desktop.arcgis.com/de/arcmap/10.3/analyze/arcpy-functions/…
    – Tim
    Commented Feb 13, 2019 at 10:46

2 Answers 2

2

If you want to add a new row and calculate as zero you should use the da.InsertCursor:

InsertCursor establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.

import arcpy
table = r'C:\Users\a48384\Desktop\00_ArcGis\Daten\netzplan'

fn_LEN_NS_FREI = "LEN_NS_FREI"
fn_NS_VERKAB_GRAD = "NS_VERKAB_GRAD"
fn_LEN_NS_KAB = "LEN_NS_KAB"
fn_ANZ_NS_KVS = "ANZ_NS_KVS"
fn_ANZ_NS_KVS_BS = "ANZ_NS_KVS_BS"
fn_ANZ_NS_KVS_NBS = "ANZ_NS_KVS_NBS"

fieldlist = [fn_LEN_NS_FREI,fn_NS_VERKAB_GRAD,fn_LEN_NS_KAB,fn_ANZ_NS_KVS,fn_ANZ_NS_KVS_BS,fn_ANZ_NS_KVS_NBS]

icur = arcpy.da.InsertCursor(table,fieldlist)
icur.insertRow([0]*len(fieldlist))
del icur
0

If you really don't hava access to arcpy.da cursors (which are available since at least the very old version 10.1 though) and want to stick with old cursors, then you first need to use an arcpy.InsertCursor in order to create your rows. Then you'll use an arcpy.UpdateCursor to update the exisiting ones.

First you should know how many rows you want to insert, then you can do something like (assuming you want (e.g.) 10 rows):

cur = arcpy.InsertCursor(tbNetzplan)

row_number = 10
for i in range(row_number):
    row = cur.newRow()
    row.setValue(fn_LEN_NS_FREI, VALUE1)
    row.setValue(fn_LEN_NS_KAB, VALUE3)
    row.setValue(fn_ANZ_NS_KVS, VALUE4)
    row.setValue(fn_ANZ_NS_KVS_BS, VALUE5)
    row.setValue(fn_ANZ_NS_KVS_NBS, VALUE6)
    row.setValue(fn_ANZ_NS_KVS_NULL, VALUE7)
    cur.insertRow(row)
1
  • I just realized my code had arcpy.UpdateCursor instead of arcpy.InsertCursor. Edited.
    – umbe1987
    Commented Feb 13, 2019 at 13:09

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.