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\Desktop00円_ArcGis\Daten'
tb_name = '\netzplan'
arcpy.CreateTable_management(tb_path,tb_name)
tbNetzplan = r'c:\users\a48384\desktop00円_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 Answers 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\Desktop00円_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
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)
-
I just realized my code had arcpy.UpdateCursor instead of arcpy.InsertCursor. Edited.umbe1987– umbe19872019年02月13日 13:09:49 +00:00Commented Feb 13, 2019 at 13:09
arcpy.da.InsertCursor
with a column list that only includes your required columns, your work is done.