Select to view content in your preferred language

Do I want to set the database connection as an environment in order to loop through feature classes?

3071
14
Jump to solution
12-15-2017 11:04 AM
PaulHuffman
Frequent Contributor

After mass loading a bunch of feature classes into a enterprise geodatabases (10.3.1, MSSQL),  I need to add privileges for a role to many of them.  I tested ChangePrivileges  with a single feature class,  it worked,  so now I thought I would extend this by getting a list of feature classes, then using a For loop on the list to grant the privileges.  But it didn't wok to set up the correct database connection as a env.workspace.  What's the correct way to loop through a enterprise geodatabase to add permissions?  Or do I want to do this in a totally different way?  The following script gives a TypeError: 'NoneType' object is not iterable  on debugging line 17, indicating I didn't get anything into the list. 

# ModPriv2lp.py
# Testing how to modify enterprise geodatabase privileges with python
# Paul Huffman, 2017-12-14

import arcpy
from arcpy import env
env.workspace = "Database Connections/Streams as GisOwner.sde"

#get list of feature classes in geodatabase.
fclist = []
fclist = arcpy.ListFeatureClasses ("Database Connections/Streams as GisOwner.sde")

#datasetName = "Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers"
#datasetName = "Streams.GISOWNER.str37_route_rivers"
#arcpy.ChangePrivileges_management(datasetName, "GISUsers", "GRANT", "AS_IS")

for fc in fclist:
    print fc
    #arcpy.ChangePrivileges_management(fc, "GISUsers", "GRANT", "AS_IS")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

When using print with Python 3.x, you will want to put parenthesis around the print statement:

print("Print message")

View solution in original post

14 Replies
PaulHuffman
Frequent Contributor

I got this to run in ModelBuilder with iteration, but unfortunately, the export to python isn't much help in figuring out the python syntax for the workspace environment. 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Paul,

Once you set your environment workspace, the List functions (i.e. ListFeatureClasses, ListDatasets, etc) will automatically use that workspace.  You're trying to set the workspace in the ListFeatureClasses function for the wild card parameter. Try the following:

import arcpy
from arcpy import env
env.workspace = "Database Connections/Streams as GisOwner.sde"

#get list of feature classes in geodatabase.
fclist = []
fclist = arcpy.ListFeatureClasses ()
PaulHuffman
Frequent Contributor

Thanks Jake.  But I have tried that syntax as well.  Still getting the error " for fc in fclist:
TypeError: 'NoneType' object is not iterable"

# -*- coding: utf-8 -*-
# ModPriv2lp.py
# Testing how to modify enterprise geodatabase privileges with python
# Paul Huffman, 2017-12-14

# Set the necessary product code
import arceditor

import arcpy
from arcpy import env
env.workspace = "Database Connections/Streams as GisOwner.sde"

#get list of feature classes in geodatabase.
fclist = []
#fclist = arcpy.ListFeatureClasses ("Database Connections/Streams as GisOwner.sde")
fclist = arcpy.ListFeatureClasses ()

#The folling line worked by setting connection + feature class in a string for ChangePrivileges
#datasetName = "Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers"
#datasetName = "Streams.GISOWNER.str37_route_rivers"
#arcpy.ChangePrivileges_management(datasetName, "GISUsers", "GRANT", "AS_IS")

for fc in fclist:
    print fc
    #arcpy.ChangePrivileges_management(fc, "GISUsers", "GRANT", "AS_IS")
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Check the Streams as GisOwner.sde connection is connected as the correct user.  You can do this by right-clicking on the database connection > Connection Properties.  It could be the user does not have privileges to any of the feature classes.

PaulHuffman
Frequent Contributor

Thanks Jake,  but it worked when I ran a script like this on just one feature class.  There must be a typo somewhere. 

# ModPriv.py
# Testing how to modify enterprise geodatabase privileges with python
#Paul Huffman, 2017-12-14

import arcpy

datasetName = "Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers"
arcpy.ChangePrivileges_management(datasetName, "GISUsers", "GRANT", "AS_IS")
0 Kudos
PaulHuffman
Frequent Contributor

Well, now the short test version that changed just one feature class doesn't run.  I keep getting "ExecuteError: ERROR 000229: Cannot open Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers
Failed to execute (ChangePrivileges).

The Model with iteration version continues to update the privileges through the database connection.  Export to python shows that the syntax is :

# Process: Change Privileges
arcpy.ChangePrivileges_management("'Database Connections\\Streams as GisOwner.sde\\Streams.GISOWNER.str39_route_rivers'", "GISUsers", "GRANT", "AS_IS")

I noticed that ModelBuilder used both double and single quotes around the connection string, but this didn't work for me in python when I tried it. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am not sure what the best way is, but I would say the most Pythonic way is using Walk—Help | ArcGIS Desktop 

PaulHuffman
Frequent Contributor

Hey,  I've used Walk before.  I'll give it a try in this situation. 

0 Kudos
PaulHuffman
Frequent Contributor

Here's an idea.  I just tried the three line version of the script from IDLE,  and still get "ERROR 000229: Cannot open Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers."  Yet I can copy those three lines into  ArcCatalog's python window and it successfully ran.  And the model with iterator version runs successfully.  Do you suppose that setting datasetName = "'Database Connections/Streams as GisOwner.sde/Streams.GISOWNER.str37_route_rivers'"   in IDLE isn't working because that connection string doesn't mean anything to the OS,  the OS can't find that connection,  while running it in ArcCatalog can find the correct relative path and connection?  Dropbox - Screenshot 2017-12-18 15.21.59.png 

0 Kudos