using cursor to load data

1513
8
Jump to solution
03-20-2020 02:16 AM
PSGeo
by
Occasional Contributor

Hi,

using ArcMap 10.5.

look i want to simply load the data from one gdb to other gdb... but on my first attempt the geometry of polygons and lines was not uploaded. I read in the forums that i needed to include the "SHAPE@". I tried as such, but the result is bad... i cannot understand how to fix it. The error message is: AttributeError: 'unicode' object has no attribute editable.

can someone suggest me something?

cheers

P

        env.workspace = datasets[0]
        fcs = arcpy.ListFeatureClasses()

        for fc in fcs:
            #print '\t', fc
            count=count_records(fc)   # counts the features within each FC
            if count<>"0":
                listfields = arcpy.ListFields(fc)
                for l in range(len(listfields)):
##                    listfields = listfields.name
##                    print listfields
                    listfields = [f.name for f in listfields if f.editable and f.type != "Geometry"] + ["SHAPE@"]
                with arcpy.da.SearchCursor(fc,listfields) as cur:

                    with arcpy.da.InsertCursor(GDBDwgs + "\\" + fc, l) as icur:

                        for row in cur:
                            print '\t', fc
                            icur.insertRow(row)
                    del icur
                del cur
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Pedro,

You could use the truncate and then append tool to load the data from one feature class to another.  First execute the truncate so that you will not append duplicate features.

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

There is no need to do things through a cursor.

If you have a need to copy featureclasses from one gdb to another and you want to do it with some changes in the output name perhaps, look at existing tools and their code examples.

Do recognize that many tools can be 'batched' without coding.

Copy—Data Management toolbox | Documentation 

Feature Class to Feature Class—Conversion toolbox | Documentation 

Examine the existing tools within the Tools structure, particular those in the Conversion and Data Management toolsets.

Anatomy of a tool reference page—ArcGIS Help | Documentation 

Code examples ... if needed at all ... are contained within

PSGeo
by
Occasional Contributor

Hi Dan,

thanks for the feedback. But the links are all for arcgisPro, it's not my case. Plus, I have already a destination GDB with feature classes inside. my goal in the end is that i run the script for several gdbs that will feed a single gdb (existing one not creating a new one).

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Pedro,

You could use the truncate and then append tool to load the data from one feature class to another.  First execute the truncate so that you will not append duplicate features.

PSGeo
by
Occasional Contributor

I tried the append... but it gave this error: 

Traceback (most recent call last):
File "\\eusc.europa.eu\ATLAS\Home\psoares\Documents\python\filesupporttools\CopyfGDBtGDB_append_version.py", line 63, in <module>
arcpy.Append_management(fcs,GDBDwgs,"NO_TEST","","" )
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 4271, in Append
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Table View.
ERROR 000840: The value is not a Raster Layer.
Failed to execute (Append).

fcs = arcpy.ListFeatureClasses()


        for fc in fcs:
            print '\t', fc
            count=count_records(fc)   # counts the features within each FC
            if count<>"0":
                arcpy.Append_management(fcs,GDBDwgs,"NO_TEST","","" )

this is really hard

0 Kudos
JakeSkinner
Esri Esteemed Contributor

What is your GDBDwgs set to?  

You may want to first try executing the Append tool in ArcMap.  Then go to the Results window > right-click on the result > Copy As Python Snippet.  This will copy the python syntax to your clipboard that you can then paste into your script.

0 Kudos
PSGeo
by
Occasional Contributor

Sorry... it worked, i forgot to point it the table. now it's like this and it worked... well i just test one time i am doing now test with real data.

        for fc in fcs:
            print '\t', fc
            count=count_records(fc)   # counts the features within each FC
            targetfc = GDBDwgs + "\\" + fc
            if count<>"0":
                arcpy.Append_management(fc,targetfc,"NO_TEST","","" )
0 Kudos
DanPatterson_Retired
MVP Emeritus

the equivalent tools exist in arcmap

0 Kudos
CaseyFrost
New Contributor II

Your code is hard for me to read but I had a similar issue and I solved it by placing the 'Shape@' field in my list of fields for the insert cursor, as well as in my list of fields for the search cursor. 

new_fields = [field.name if field.name != 'Shape' else 'Shape@' for field in arcpy.ListFields(new_path)]
old_fields = [field.name if field.name != 'Shape' else 'Shape@' for field in arcpy.ListFields(old_path)]

 

0 Kudos