The data i was working with was just shapefiles and after i commented out the editor lines it ran fine.
The data I will be working is in a SQL Sever Enterprise Geodatabase on my desktop so because the dataset is not registered as versioned with the database i had to change this line
I did how ever run into something weird. I select a parcel that has an address point and run the code it runs fine. The attributes get updated but if i select a different parcel with an address point and run the code again it updates the current select features attribute but the previous address points attributes get deleted...some how the first address point attributes doesn't save...?
I also noticed that when i exit the mxd and reopen. The last address point didn't save, i check the attributes and it's blank/null.
Here is what i am working with.
import arcpy
import os
import pythonaddins
# Assign string literal layer names to variables to make them easier to change with just one line of code.
# Good practice for template code that you may not want to do a search and replace to adapt it for new layers.
parcels = "Taxparcels"
addressPoints = "TonyTwoWay.DBO.CCAP_Test"
#Get the current map document and the first data frame.
mxd = arcpy.mapping.MapDocument('current')
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Check to make sure user had at least one parcel selected.
parcelsCount = int(arcpy.GetCount_management(parcels).getOutput(0))
if 0 < parcelsCount < 2:
lyr = arcpy.mapping.ListLayers(mxd, addressPoints, df)[0]
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Connection to dsd15_sqlexpress.sde"
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True,False)
# Start an edit operation
edit.startOperation()
#Select Address Points within selected parcel
arcpy.SelectLayerByLocation_management(addressPoints, "HAVE_THEIR_CENTER_IN", parcels)
with arcpy.da.SearchCursor(parcels, ['ACCOUNT','SiteStreet']) as singleCursor:
for singleRow in singleCursor:
ACCOUNT = singleRow[0]
SiteStreet = singleRow[1]
with arcpy.da.UpdateCursor(addressPoints, ['Account', 'SiteStreet']) as singleCursor:
for singleRow in singleCursor:
singleRow[0] = ACCOUNT
singleRow[1] = SiteStreet
singleCursor.updateRow(singleRow)
# Stop the edit operation.
edit.stopOperation()
print('Parcel address update is complete.')
elif 1 < parcelsCount < 1000:
#Create in memory parcel layer
selectedParcels = arcpy.MakeFeatureLayer_management(parcels, "selectedParcels")
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name == addressPoints:
addressLayer = lyr
if lyr.name == parcels:
parcelsLayer = lyr
workspace = addressLayer.workspacePath
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True,False)
# Start an edit operation
edit.startOperation()
with arcpy.da.SearchCursor(parcels, ['ACCOUNT', 'SiteStreet', 'OBJECTID']) as parcelCursor:
for parcelRow in parcelCursor:
ACCOUNT = parcelRow[0]
SiteStreet = parcelRow[1]
OID = parcelRow[2]
OID = str(OID)
arcpy.SelectLayerByAttribute_management("selectedParcels", "NEW_SELECTION", "OBJECTID= "+OID)
#Select Address Points by location, within selected Parcels.
arcpy.SelectLayerByLocation_management(addressPoints,"HAVE_THEIR_CENTER_IN","selectedParcels")
with arcpy.da.UpdateCursor(addressPoints, ['Account', 'SiteStreet']) as addressCursor:
for addressRow in addressCursor:
addressRow[0] = Account
addressRow[1] = SiteStreet
addressCursor.updateRow(addressRow)
del ACCOUNT, SiteStreet
# Stop the edit operation.
edit.stopOperation()
arcpy.Delete_management("selectedParcels")
print('Parcel address update is complete.')
else:
print('Please select a valid number of parcels.')