Basic arcpy script (ArcMap 10.8), I'm trying to add field delimiters for a query, however it's adding double-quotes " "
instead of square brackets [ ]
around my field name, which then makes my query fail, as the feature class is in a personal geodatabase which requires the square brackets.
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layername = "my_layer"
>>> layer = arcpy.mapping.ListLayers(mxd, layer_name)[0]
>>> print layer
my_layer
>>> field_delimiter = arcpy.AddFieldDelimiters(layer, "OBJECTID")
>>> print field_delimiter
"OBJECTID"
I have realised that I can pass the path to the actual feature class to the AddFieldDelimiters()
tool, so this is my current work-around solution:
>>> field_delimiter = arcpy.AddFieldDelimiters(r"C:\Temp\mygdb.mdb\MyFC", "OBJECTID")
>>> print field_delimiter
[OBJECTID]
However, I'd like to know if I'm doing something wrong in the syntax of the layer based approach, and how to fix it. If I continue with the workaround I'll have to include extra functionality to determine what the path to the data actually is (which is fine I guess, just adds further steps to the process)
How can I use the AddFieldDelimiters()
on a layer within my MXD?
As requested in comments, here is working test version of my code, including the query, that was failing because of the " "
instead of [ ]
import arcpy
layer = "Layer_in_MXD"
selected_oid = 36
query = "{} <> {}".format(arcpy.AddFieldDelimiters(layer, arcpy.Describe(layer).OIDFieldName), selected_oid)
arcpy.AddMessage(query)
with arcpy.da.SearchCursor(layer, ["OID@", "SHAPE@"], query) as cursor:
for row in cursor:
row_oid, row_geom = row
which outputs the following:
Executing: xTestPython
Start Time: Wed Feb 21 06:17:24 2024
Running script xTestPython...
"OBJECTID" <> 36
Failed script xTestPython...
Traceback (most recent call last):
File "C:\Test\testpython.py", line 8, in <module>
for row in cursor:
RuntimeError: Data type mismatch in criteria expression.
Failed to execute (xTestPython).
Failed at Wed Feb 21 06:17:25 2024 (Elapsed Time: 0.36 seconds)
If I make the query use square brackets (as Esri documentation says Personal Geodatabases require square brackets) then the query works without issue. This is done either by hard-coding the square brackets (not a long-term solution as the source data won't always be Personal GDB), or by making the Add Field Delimiters first detect the full source path of the layer (my current work-around)
Executing: xTestPython
Start Time: Wed Feb 21 06:20:47 2024
Running script xTestPython...
[OBJECTID] <> 36
Completed script xTestPython...
Succeeded at Wed Feb 21 06:20:47 2024 (Elapsed Time: 0.41 seconds)
From Esri Docs for Add Field Delimiters:
The field delimiters used in an SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotation marks (" "), personal geodatabases use square brackets ([ ]), and enterprise geodatabases don't use field delimiters. The function can take away the guess work in ensuring that the field delimiters used with your SQL expression are the correct ones.
AddFieldDelimiters()
to construct queries. I think they were necessary for writing flexible data source type code until 5-10 years ago but then ArcMap started to support field names without delimiters in expressions and I think ArcGIS Pro has never required them.