<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How do I check if a domain already exists? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335317#M26236</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;how do I get the domains description in a SDE connection?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or how do I check if a domain already exists?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Found this code but it does not run:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; desc = arcpy.describe(SDEConnection)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;AttributeError: 'module' object has no attribute 'describe'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.env = SDEConnection

desc = arcpy.describe(SDEConnection)
domains = desc.Domains
for domain in domains:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 19 Sep 2011 19:04:38 GMT</pubDate>
    <dc:creator>JoseSanchez</dc:creator>
    <dc:date>2011-09-19T19:04:38Z</dc:date>
    <item>
      <title>How do I check if a domain already exists?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335317#M26236</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;how do I get the domains description in a SDE connection?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or how do I check if a domain already exists?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Found this code but it does not run:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; desc = arcpy.describe(SDEConnection)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;AttributeError: 'module' object has no attribute 'describe'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.env = SDEConnection

desc = arcpy.describe(SDEConnection)
domains = desc.Domains
for domain in domains:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Sep 2011 19:04:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335317#M26236</guid>
      <dc:creator>JoseSanchez</dc:creator>
      <dc:date>2011-09-19T19:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I check if a domain already exists?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335318#M26237</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What RDBMS are you using (i.e. SQL Server, Oracle, PostgreSQL)?&amp;nbsp; Below is an example on how to do this using SQL Server.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can download the 'pyodbc' library &lt;/SPAN&gt;&lt;A href="http://code.google.com/p/pyodbc/downloads/list" rel="nofollow noopener noreferrer" target="_blank"&gt;here&lt;/A&gt;&lt;SPAN&gt;.&amp;nbsp; This will allow you to connect to SQL Server using python.&amp;nbsp; You can then execute the following code to find all existing domains in your SDE geodatabase:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import pyodbc
cnxn = pyodbc.connect('Driver={SQL Server Native Client 10.0};UID=&amp;lt;username&amp;gt;;PWD=&amp;lt;password&amp;gt;;SERVER=&amp;lt;server name&amp;gt;;DATABASE=&amp;lt;database&amp;gt;;')
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row[0]

cursor.close()
cnxn.close()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You will need to update the connection string &lt;/SPAN&gt;&lt;STRONG&gt;(cnxn = pyodbc.connect('Driver={SQL Server Native Client 10.0};UID=&amp;lt;username&amp;gt;;PWD=&amp;lt;password&amp;gt;;SERVER=&amp;lt;server name&amp;gt;;DATABASE=&amp;lt;database&amp;gt;;')&lt;/STRONG&gt;&lt;SPAN&gt; with the correct username, password, server, and database.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:53:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335318#M26237</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I check if a domain already exists?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335319#M26238</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;arcpy methods are CaSe SensiTive at ArcGIS 10. Use "Describe" instead of "describe".&amp;nbsp; The following works for me on our SDE server:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.env = SDEConnection

desc = arcpy.Describe(SDEConnection)
domains = desc.Domains
for domain in domains:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:53:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335319#M26238</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T15:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I check if a domain already exists?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335320#M26239</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

workspace = env.workspace = r"Z:\Test.gdb"

desc = arcpy.Describe(workspace)
domains = desc.domains

for domain in domains:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(domain):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The issue I have is the behavior of the 'Exists' method'. When I set a conditional sentence to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if arcpy.Exists(domain):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried the reverse:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if not arcpy.Exists(domain):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print domain
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:53:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335320#M26239</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2021-12-11T15:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I check if a domain already exists?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335321#M26240</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000021000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Exists&lt;/A&gt;&lt;SPAN&gt; 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.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regardless, you already know that the domains are in the database as you have a list of them, i.e. &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;domains = desc.domains&lt;/SPAN&gt;&lt;SPAN&gt;, 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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if desc.domains:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'There be domains ahead...'
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'No domains here!'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:53:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-check-if-a-domain-already-exists/m-p/335321#M26240</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T15:53:21Z</dc:date>
    </item>
  </channel>
</rss>

