I am writing a script to use an updatecursor. The feature class is locked so I am versioning the database, creating a feature layer, and reconciling versions. I have taken the skeleton for my script from this thread. Debugging RuntimeError: workspace already in transaction mode from arcpy.da.UpdateCursor and ArcSDE feature classes?
The main difference is that I already had an SDE connection file in my directory. Should this be re-created each time the script is executed to avoid this error?
__author__ = 'Administrator'
from arcpy import *
import GetSRConfig
import os
import time
def GetDateTimeString(n = None):
""" format a datetime to string """
if(n==None):
s = time.strftime("%Y%m%d%H%M%S", time.localtime())
else:
s = time.strftime("%Y%m%d%H%M%S", time.localtime())
if((isNumeric(n)==True) and ((n>4) and (n<14))):
s = s[0:n]
else:
s = s[0:14]
return s
def isNumeric(s):
b = True
try:
i = float(s)
except: # not numericelse: # numeric
b= False
return b
#Locals
P6featureName = GetSRConfig.SDELayer
Parent = "dbo.DEFAULT"
version = "myVersion" + GetDateTimeString(12)
featureLyr = "lyr" + GetDateTimeString(12)
print version
# Server = ***
# Service = ***
user ="dbo"
# Pass = ***
# SDE = 'E:\C_Drive_files\Administrator'\311Request\data\ServiceRequest.sde'
temploc = ""
fil = "SDETempConn"
env.overwriteOutput = True
#Create Version
print "Creating version"
arcpy.CreateVersion_management (GetSRConfig.SDEConnFile, Parent, version, "PUBLIC")
VersionName = user.upper() + "." + version
#Create new connection
workspace = os.path.join (temploc, fil + ".sde")
print workspace
#Layers
P6feature = os.path.join (workspace, P6featureName)
arcpy.MakeFeatureLayer_management (GetSRConfig.SDELayer, featureLyr)
#Start editing
print "Initiating editing"
edit = arcpy.da.Editor (GetSRConfig.SDEConnFile)
edit.startEditing ()
edit.startOperation()
#Test Cursor
print "Testing cursor"
P6Cursor = arcpy.da.UpdateCursor (featureLyr, ["VehicleNam"])
for row in P6Cursor:
print row[0]
del row
del P6Cursor
#Stop/save edits
edit.stopOperation()
print "Stopping editing"
edit.stopEditing("True")
#Switch to version
print "Switching version"
arcpy.ChangeVersion_management(featureLyr, "TRANSACTIONAL", Parent)
#Reconcile and post
print "Reconciling and posting"
arcpy.ReconcileVersions_management (GetSRConfig.SDEConnFile, "", Parent, VersionName, with_post = "POST", with_delete = "DELETE_VERSION")