Avoiding errors when adding rows to blank table using ArcPy?

1761
11
Jump to solution
04-20-2017 04:07 PM
DavidSouth2
New Contributor

I am having problems when using ArcPy with ArcGIS Desktop to add in a set number of rows into a .dbf table.

What should I do to prevent these errors?

Here is a sample code that gives me errors:

##########setup###########
import arcpy, os
from arcpy import env


##########parameters##########

ws = r'C:\Users\ddsouth\Documents\Python_tests'
env.workspace = ws

newFile = "adding_table_rows_test_5"
newFileFull = os.path.join(ws, newFile) + ".dbf"

###########code###############

#create the table
arcpy.CreateTable_management(ws, newFile)
arcpy.AddField_management(newFileFull, "RouteName", "TEXT", "", "", 15)
arcpy.AddField_management(newFileFull, "DepotName", "TEXT", "", "", 15)

#edit the table
cursor = arcpy.da.InsertCursor(newFile, ["ROUTENAME"])

for q in range (0,10):
 cursor.insertRow((str(1),))
del cursor

print "Completed."

The creation of the table goes fine the first time, then when it comes to the edit portion I get the following error message:

Runtime error Traceback (most recent call last):  File "<string>", line 26, in <module>SystemError: error return without exception set

Then if I run the program again, I get a different error, this time during the creation of the table, even if I have the "Overwrite the outputs of geoprocessing operations" option selected:

Runtime error  Traceback (most recent call last):   File "<string>", line 18, in <module>   File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 15715, in CreateTable     raise e ExecuteError: ERROR 001143: Background server threw an exception.

I also noticed that while the tables I created appear in ArcCatalog, they do not appear in Windows Explorer, there is only a LOCK file while ArcMap is open.

What should be done? Thank you!

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Just to be clear, the change you need is to set the output pathname with a #dbf extension. This is only necessary when the output workspace (ws) is a folder, with no extension there is an assumption you have created an info folder with the CreateWorkspace command or by running a tool that creates an INFO directory. This is historical compatibility; these days you should avoid INFO table format unless you have no way around it (for example, coverage and Esri grid formats).

newFile = "adding_table_rows_test_3.dbf"

I also would not use this:

cursor.insertRow((str(1),))

but instead, this. (I just like lists over tuples, you can simply do more with them.)

cursor.insertRow(["1"])

View solution in original post

11 Replies
JoshuaBixby
MVP Esteemed Contributor

Cross-posted to StackExchange:  Avoiding errors when adding rows to blank table using ArcPy?

Since this is a question, it is best to mark it as a question instead of a discussion.

0 Kudos
DavidSouth2
New Contributor

Thanks, I will remember this for next time. I am still getting used to how this site works.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Do you mean to be working with Info or dBASE tables?  Your code as written is creating an Info table and trying to insert records into it.  I am surprised the Add Field calls aren't generating errors.

0 Kudos
curtvprice
MVP Esteemed Contributor

I noticed you opened the cursor on newFile, not newFileFull, which is missing the .dbf extension. Mr Bixby is right on that a table path in a folder with no .dbf tacked on is interpreted as an INFO table path. Good old INFO. A humble personal financial database put into service mapping the world.

I really like to make table views both for performance reasons when I run multiple tools on a single input (speedier validation) but also this can insulate your from path specification errors like that.

#create the table
tbl = arcpy.CreateTable_management(ws, newFile)
tv = arcpy.MakeTableView_management(tbl, "tv")
arcpy.AddField_management(tv, "RouteName", "TEXT", "", "", 15)
arcpy.AddField_management(tv, "DepotName", "TEXT", "", "", 15)

#edit the table
cursor = arcpy.da.InsertCursor(tv, ["ROUTENAME"])

...

# clean up tv
arcpy.Delete_management(tv)

					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
NeilAyres
MVP Alum

Sorry for hijacking this thread....

Back in the day, my bookshelf contained a photocopied version of "Understanding Info" by Henco software.

It was my bible in those days.

curtvprice
MVP Esteemed Contributor

PROGRAM SECTION EVEN and RELATE FILL ORDERED... those were the days!

AML forever, brother!

DavidSouth2
New Contributor

Hello Curtis,

Thank you for the response. I have changed my code to look like this:

##########setup###########
import arcpy, os
from arcpy import env

##########parameters##########

ws = r'C:\Users\ddsouth\Documents\Python_tests'
env.workspace = ws

newFile = "adding_table_rows_test_3"

###########code###############
#create the table
tbl = arcpy.CreateTable_management(ws, newFile)
tv = arcpy.MakeTableView_management(tbl, "tv")
arcpy.AddField_management(tv, "RouteName", "TEXT", "", "", 15)
arcpy.AddField_management(tv, "DepotName", "TEXT", "", "", 15)

#edit the table
cursor = arcpy.da.InsertCursor(tv, ["ROUTENAME"])

for q in range (0,10):
    cursor.insertRow((str(1),))
del cursor

#clean up tv
arcpy.Delete_management(tv)

print "Completed."

Yet I still get the error on the "cursor.insertRow((str(1),))" line:

Runtime error

...

SystemError: error return without exception set

Is there a way to fix this? I really appreciate the help.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Do you mean to be working with Info or dBASE tables?  Your code as written is still creating an Info table and trying to insert records into it.  Before I get into explaining Info-related issues with cursors, I want to make sure you want to be working with Info tables.  Do you have to be working with Info tables or would dBASE work as well?

curtvprice
MVP Esteemed Contributor

Just to be clear, the change you need is to set the output pathname with a #dbf extension. This is only necessary when the output workspace (ws) is a folder, with no extension there is an assumption you have created an info folder with the CreateWorkspace command or by running a tool that creates an INFO directory. This is historical compatibility; these days you should avoid INFO table format unless you have no way around it (for example, coverage and Esri grid formats).

newFile = "adding_table_rows_test_3.dbf"

I also would not use this:

cursor.insertRow((str(1),))

but instead, this. (I just like lists over tuples, you can simply do more with them.)

cursor.insertRow(["1"])