Select to view content in your preferred language

Add new row to empty file geodatabase table

3903
5
Jump to solution
03-04-2013 11:34 AM
ClaudineSicker
Occasional Contributor
I am using ArcGIS 10.1 and have created a script that checks to see if a file geodatabase table is empty. It is a table with no spatial referencing.

The code I have thus far can add rows to tables that already have a record in it, but it will not add a new row to an empty table.

Can this be done? Would it be better accomplished if it were a dbf file rather than a geodatabase table?

Snippet of code looks like this:

    addRow =arcpy.da.InsertCursor(Variable1,("Frequency", "Used", "Region"))
    result = arcpy.GetCount_management(Variable1)
    print result
    print addRow
    if result == 0:
    [indent]addRow.insertRow((0, "Locally", 3))[/indent] 
    else:
    [indent]do something else[/indent]

result and addRow printouts are correct. Prior tests taking out the if, else statements and just running the addRow.insertRow statement showed that tables that have at least one record get appended with a new row.

So I am of the opinion that I cannot add a new row using this approach. Any other ideas as to how I can add a row to an empty table.

Many thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Result is not an integer you can compare how you are. You need this.
result = int(arcpy.GetCount_management(Variable1).getOutput(0))


Also don't forget to add this at the end of your script.
del addRow

View solution in original post

0 Kudos
5 Replies
MathewCoyle
Honored Contributor
Does the empty table have the fields you are trying to populate?
0 Kudos
ClaudineSicker
Occasional Contributor
Does the empty table have the fields you are trying to populate?


Yes. the fields referenced in the addRow variable ("Frequency", "Used", "Region")

I want to populate them with (0, "Locally", 3)
0 Kudos
MathewCoyle
Honored Contributor
Result is not an integer you can compare how you are. You need this.
result = int(arcpy.GetCount_management(Variable1).getOutput(0))


Also don't forget to add this at the end of your script.
del addRow
0 Kudos
ClaudineSicker
Occasional Contributor
Result is not an integer you can compare how you are. You need this.
result = int(arcpy.GetCount_management(Variable1).getOutput(0))


Also don't forget to add this at the end of your script.
del addRow


Yippee. It worked.

If results isn't an integer I can compare (and I am not doubting you) why did "0" (without quotes) return for the command "print results" for the empty tables?

Just trying to wrap my head around python.

Thanks a zillion. I have been going around for hours on this snippet.
0 Kudos
MathewCoyle
Honored Contributor
If you want to get as much information as possible about an output or variable you can use the following commands.
type(variable)
repr(variable)


Which shows the value returned by GetCount is an object.
>>> result = arcpy.GetCount_management(fc)
>>> type(result)
<class 'arcpy.arcobjects.arcobjects.Result'>
>>> repr(result)
"<Result '60'>"

Which you must then get the results property from. But we're still not done yet.
>>> result = arcpy.GetCount_management(fc).getOutput(0)
>>> type(result)
<type 'unicode'>
>>> repr(result)
"u'60'"


The getOutput parameter returns a unicode string, not an int. But once we are here it is a simple step to convert it to a numeric value.
>>> result = int(arcpy.GetCount_management(fc).getOutput(0))
>>> type(result)
<type 'int'>
>>> repr(result)
'60'

Hope that helps.
0 Kudos