1

I am trying to add a specific value to a cell of the second row of a feature-class-table. Doesn't work with my code:

    #First row
    rows = arcpy.InsertCursor(tb_value)
    row = rows.newRow()
    rows.insertRow(row)

    cur = arcpy.UpdateCursor(tb_value)
    for row in cur:
        row.setValue(fn_value_object, COUNTER)
        row.setValue(fn_name_area, gebietsname)
        row.setValue(fn_name_object, objektname)
        cur.updateRow(row)

    #Second row
    whereClause = """{0} = 2"""
    whereClause = whereClause.format(arcpy.AddFieldDelimiters(tb_value, "Rowid"))
    cur = arcpy.UpdateCursor(tb_value, whereClause)
    row = rows.newRow()
    rows.insertRow(row)
    for row in cur:
        row.setValue(fn_value_object, COUNTER2)

I tried it with a where-clause, but the value which is set in the second row is 0.

  • Why aren't you using an arcpy.da.InsertCursor and arcpy.da.UpdateCursor? It looks like you're not updating the row that you've changed with cur.updateRow(row) so it's not being stored. Look at the examples resources.arcgis.com/en/help/main/10.2/index.html#//… – Michael Stimson Feb 28 at 8:00
  • 1
    not checked, but I think that you are missing the flush of memory (del cur). Note that a better practice would be to use "with ... as cur :" so you make sure that the memory is cleaned. also, it is better to use arcpy.da cursor and, finally, I don't get why you insert rows. – radouxju Feb 28 at 8:00
0

Insertcursor is for inserting new rows, not updating values of existing rows (it is a bit unclear what you are asking). You are using old cursor, switch to data access cursors instead:

import arcpy

fc = r'C:\data.gdb\somefeatureclassortable'
field_to_update = 'somefieldname'
row_id_field = 'Rowid'
value = 123 #Or '123' if string and you will have to modify sql query

sql = """{0}=2""".format(arcpy.AddFieldDelimiters(datasource=fc, field=row_id_field))
with arcpy.da.UpdateCursor(fc,field_to_update,sql) as cursor:
    for row in cursor:
        row[0]=value
        cursor.updateRow(row)
  • 1
    Thank you! Works very well! The reason why I am inserting a new row, is because the table is just created in my script. So it's a new table without any rows. So my idea was to add rows and then update the default values. – Andreas Feb 28 at 8:33

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.