1
import os, arcpy, zipfile, glob 

# INPUT ZIP FILE
infile = arcpy.GetParameterAsText(0)
path = "D:\Database_Connections"

NAME = arcpy.GetParameterAsText(1)
CODE = arcpy.GetParameterAsText(2)
SOURCE = arcpy.GetParameterAsText(3)

if not os.path.exists(path):
    os.makedirs(path) 

# DATABASE CONNECTION PARAMETERS
arcpy.CreateDatabaseConnection_management ("D:\Database_Connections", "sample.sde", "SQL_Server", "@@@.@@.@@@.@@@", "DATABASE_AUTH", "dbname", "dbpassword",  "SAVE_USERNAME", "sample","#","TRANSACTIONAL","#","3/20/2018 6:45:41 PM")
arcpy.env.workspace = "D:\Database_Connections\sample.sde\sample.DBO.samplefeatureclass"


# EXTRACT ZIP FILE TO THE Database_Connections FOLDER
zip = zipfile.ZipFile(infile, 'r').extractall(r'D:\Database_Connections') 

# CODE TO SELECT POLYGON SHAPEFILE OR POLYGON FEATURE CLASS
path = "D:/Database_Connections"
fc = []
for dirpath, dirnames, filenames in arcpy.da.Walk(path,
                                              datatype="FeatureClass" or "ShapeFile",
                                              type="Polygon"):
    for filename in filenames:
        fc.append(os.path.join(dirpath, filename))

    # Open a search cursor on a feature class / table to read from  
    scur = arcpy.SearchCursor(read_fc)  
    # Open an insert cursor on the fc / table to write to  
    icur = arcpy.InsertCursor(write_fc)

CODE TO INSERT NAME, CODE AND SOURCE VALUES TO THE samplefeatureclass

I am trying to add polygon feature and the attribute (taken as an input from the user) to the existing feature class in the SDE (MSSQL database) using arcpy. I am able to add an empty row to the feature class table in SDE, can someone guide/help me to pass the user-defined attribute information (this is taken as an input from the user) and insert in the feature class table in SDE.

INPUT : Polygon shapefile & attributes defined by user as parameter.

OUTPUT : Copying/Inserting polygon feature alongwith user-defined attributes to the existing feature class in SDE.

These can be seen in the attached screenshot.

enter image description here

  • 1
    Free clue: Nothing is ever stored "in ArcSDE". At best, data is stored in a database using ArcSDE, but even that isn't true anymore, since ArcSDE no longer exists. If you frame the task of interacting with enterprise geodatabase with Python, then ArcPy issues like failure to use the "raw" designator with strings using backslashes will come to the forefront. Please remember to always specify the exact version of ArcGIS and database software in your question. – Vince Mar 29 '18 at 11:52
  • Thanks Vince for pointing about ArcSDE. I am trying to copy/insert the user-defined attribute information to the existing feature class in the Enterprise Geodatabase (MSSQL) – Rashmi Mar 29 '18 at 12:22
  • 1
    Your failures start on the fifth line, but failure to use DataAcess cursors will become a headache soon enough. Stop attempting to access the database, and just learn enough arcpy to write to a shapefile or file geodatabase; at that point, you will discover that there isn't any significant difference on insert, and that there isn't any such thing as " user-defined attribute information" just rows and columns. – Vince Mar 29 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.