Arcpy search cursor and update cursor to write values from multiple feature classes in GDB A to multiple featureclasses in GDB B

743
3
Jump to solution
08-31-2020 02:48 PM
SunitaKhanal
New Contributor II

I have created two geodatabases: Source GDB and Target GDB. Databases are basically copies of one another. Source GDB featureclasses are exported Directly from CAD whereas Target GDB contains feature classes that must be updated daily with new records from Source feature classes using search and insert cursor.

I was able to do this with a single-source feature class to a single target feature class and now I am trying to figure out how to update the entire list of target feature classes by using search and insert cursor.

In the code below I am trying to loop through both the Source and Target GDB, and if the source and target GDB are equal then create a search cursor for both source and target featureclass and insert a record if they do not exist in target featureclass. I get no error messages but my target featureclasses are not updated.

Any suggestions on what and where I am missing would be of great help.

import arcpy
from arcpy import env
import os
import sys

arcpy.env.workspace = targetGDB = r"Path to Target GDB"
targetFC = arcpy.ListFeatureClasses()
arcpy.env.worksapce = sourceGDB = r"Path to Source GDB"
sourceFC = arcpy.ListFeatureClasses()
for target in targetFC:
  # print (targetGDB, targetFC)
  for source in sourceFC:
    # print (sourceGDB, source)
    if target == source:
      curTarget=arcpy.da.SearchCursor(target, ["Handle""SHAPE@""Layer""Linetype"])
      curSource=arcpy.da.SearchCursor(source, ["Handle""SHAPE@""Layer""Linetype"])
      curInsert=arcpy.da.InsertCursor(target, ["Handle""SHAPE@""Layer""Linetype"])

      curS_list=[y[0for y in curTarget]

      for row in curSource:
        if not row[0in curS_list:
          curInsert.insertRow(row)

      del curInsert,curTarget,curSource
0 Kudos
1 Solution

Accepted Solutions
SunitaKhanal
New Contributor II

I am answering to myself and posting the code that worked for me so that it might help someone with a similar issue. I replaced ListFeatureClass by Walk function and made some other slight changes and it worked by inserting all new records from the source feature classes to the target feature classes. Feature classes in both GDBs have the same name. 

import arcpy
import os

sourceGDB = r"Path to source GDB"
targetGDB = r"Path to target GDB"

walk = arcpy.da.Walk(sourceGDB, "FeatureClass")

for dirpath, dirnames, filenames in walk:
    for fc in filenames:
        # print fc
        sourceFc = os.path.join(sourceGDB,fc)
        # print sourceFc
        targetFc = os.path.join(targetGDB,fc)
        # print targetFc
        if arcpy.Exists(targetFc):
            # print fc

            # Create Field list
            fieldList = ["Handle""SHAPE@""Layer""Linetype"]

            # Create Search and Insert Cursor
            curSource=arcpy.da.SearchCursor(sourceFc, fieldList)
            curTarget=arcpy.da.SearchCursor(targetFc, fieldList)
            curInsert=arcpy.da.InsertCursor(targetFc, fieldList)

            # Create a list of rows for target layer         
            curS_list=[row[0for row in curTarget]

            # Iterate through rows of source and target cursor and insert rows into target from source if they don't exists in target featureclasses
            for row in curSource:
                if not row[0in curS_list:
                    curInsert.insertRow(row)
            del curInsert,curSource, curTarget

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

In cases like this where you have conditional statements and no errors, it is good to add some additional debugging print statements to make sure your conditional statements are evaluating to True.  Given your cursor insertion is nested within two conditional statements, if either evaluates to False all the time, nothing will happen.

0 Kudos
SunitaKhanal
New Contributor II

Thanks for your suggestion, I will look into it in detail with the debugging option. 

0 Kudos
SunitaKhanal
New Contributor II

I am answering to myself and posting the code that worked for me so that it might help someone with a similar issue. I replaced ListFeatureClass by Walk function and made some other slight changes and it worked by inserting all new records from the source feature classes to the target feature classes. Feature classes in both GDBs have the same name. 

import arcpy
import os

sourceGDB = r"Path to source GDB"
targetGDB = r"Path to target GDB"

walk = arcpy.da.Walk(sourceGDB, "FeatureClass")

for dirpath, dirnames, filenames in walk:
    for fc in filenames:
        # print fc
        sourceFc = os.path.join(sourceGDB,fc)
        # print sourceFc
        targetFc = os.path.join(targetGDB,fc)
        # print targetFc
        if arcpy.Exists(targetFc):
            # print fc

            # Create Field list
            fieldList = ["Handle""SHAPE@""Layer""Linetype"]

            # Create Search and Insert Cursor
            curSource=arcpy.da.SearchCursor(sourceFc, fieldList)
            curTarget=arcpy.da.SearchCursor(targetFc, fieldList)
            curInsert=arcpy.da.InsertCursor(targetFc, fieldList)

            # Create a list of rows for target layer         
            curS_list=[row[0for row in curTarget]

            # Iterate through rows of source and target cursor and insert rows into target from source if they don't exists in target featureclasses
            for row in curSource:
                if not row[0in curS_list:
                    curInsert.insertRow(row)
            del curInsert,curSource, curTarget