Select to view content in your preferred language

How do I check if a domain already exists?

3595
4
09-19-2011 12:04 PM
JoseSanchez
Regular Contributor
Hi all,

how do I get the domains description in a SDE connection? 

Or how do I check if a domain already exists?

Found this code but it does not run:
desc = arcpy.describe(SDEConnection)
AttributeError: 'module' object has no attribute 'describe'
>>>



arcpy.env = SDEConnection

desc = arcpy.describe(SDEConnection)
domains = desc.Domains
for domain in domains:
    print domain
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
What RDBMS are you using (i.e. SQL Server, Oracle, PostgreSQL)?  Below is an example on how to do this using SQL Server.

You can download the 'pyodbc' library here.  This will allow you to connect to SQL Server using python.  You can then execute the following code to find all existing domains in your SDE geodatabase:

import pyodbc
cnxn = pyodbc.connect('Driver={SQL Server Native Client 10.0};UID=<username>;PWD=<password>;SERVER=<server name>;DATABASE=<database>;')
cursor=cnxn.cursor()

cursor.execute("SELECT items.Name AS 'Domain Name', items.Definition.value('(/*/Owner)[1]','nvarchar(max)') \
AS 'Owner' FROM sde.GDB_ITEMS AS items INNER JOIN sde.GDB_ITEMTYPES AS itemtypes ON \
items.Type = itemtypes.UUID WHERE itemtypes.Name IN ('Coded Value Domain', 'Range Domain')")

rows = cursor.fetchall()

for row in rows:
    print row[0]

cursor.close()
cnxn.close()


You will need to update the connection string (cnxn = pyodbc.connect('Driver={SQL Server Native Client 10.0};UID=<username>;PWD=<password>;SERVER=<server name>;DATABASE=<database>;') with the correct username, password, server, and database.
0 Kudos
Luke_Pinner
MVP Regular Contributor
arcpy methods are CaSe SensiTive at ArcGIS 10. Use "Describe" instead of "describe".  The following works for me on our SDE server:
arcpy.env = SDEConnection

desc = arcpy.Describe(SDEConnection)
domains = desc.Domains
for domain in domains:
    print domain
0 Kudos
MikeMacRae
Regular Contributor
Hi,

I'm just digging up this old thread. I am trying to check the existence of domains in my file geodatabase which contains 4 domains (automatically populated after I created some annotation feature classes). My code is as follows:

import arcpy

workspace = env.workspace = r"Z:\Test.gdb"

desc = arcpy.Describe(workspace)
domains = desc.domains

for domain in domains:
    if arcpy.Exists(domain):
        print domain


The issue I have is the behavior of the 'Exists' method'. When I set a conditional sentence to:

if arcpy.Exists(domain):
        print domain


the domains does not print. I'm thinking the code reads like 'If the domain exists, then print out the domain'. Because I have 4 domains, it should print these, but it doesn't.

I tried the reverse:

if not arcpy.Exists(domain):
        print domain


In this case, the domain gets printed out which seems counter intuitive. To me this reads like 'If the domain does not exists, then print the domain which should return nothing because it does not exist'

The problem I have with the code example in the last post is, it doesn't use the 'Exists' method. I want to test if any domains exists, instead of using a conditional statement to test if specific domains exist.

Thanks,
Mike
0 Kudos
Luke_Pinner
MVP Regular Contributor
The Exists method checks for the existence of data elements, i.e. "feature classes, tables, datasets, shapefiles, workspaces, layers, and files". A domain is a rule, not a data element. 

Regardless, you already know that the domains are in the database as you have a list of them, i.e. domains = desc.domains, there's no need to double check. If you want to check that there are any domains at all, test for an empty list, perhaps something like:
if desc.domains:
    print 'There be domains ahead...'
else:
    print 'No domains here!'
0 Kudos