Monday 21 September 2015

esri geodatabase - How to get a search cursor on a particular version in arcpy


I have a versioned database in ArcGIS and I am wondering how can I tell arcpy what version to query. The closest I have got to an answer has been building a particular connection file that specifies the version to connect to, or use ChangeVersion_management which only works on layers and views.


I can easily do that in a published map on the web using ArcGIS JS API by setting the version using layer.setGDBVersion(version)



Answer



You are on the right track. You just need to use MakeFeatureLayer to convert the feature class to a layer. I created a version and did an edit and then tried the following:


>>> arcpy.env.workspace = r'Database Connections\gisadmin.sde'
>>> arcpy.MakeFeatureLayer_management(r'Database Connections\gisadmin.sde\GISADMIN.Buildings', 'fl')
>>> arcpy.ChangeVersion_management('fl', "TRANSACTIONAL", "GISADMIN.Oli")
>>> arcpy.GetCount_management('fl')


>>> arcpy.ChangeVersion_management('fl', "TRANSACTIONAL", "sde.DEFAULT")

>>> arcpy.GetCount_management('fl')

>>> with arcpy.da.SearchCursor('fl', flds) as cur:
...

Also, there is an advantage to starting from a pre-created connection file vs creating one in code in that the password is not visible in the connection file. You would only need to pre-create the connection file per user since you can change the version in code.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...