Hi everyone,
can someone please tell me what is wrong with my syntax here? This should be very simple but any time I run this script tool it does not like the syntax of my definitionQuery.. I can't understand what the problem is
I have tried all of these and it wont work:
lyr.definitionQuery = '"BANDID" = \'GRNN00A\''
lyr.definitionQuery = "BANDID" = 'GRNN00A'
lyr.definitionQuery = """"BANDID" = 'GRNN00A'"""
lyr.definitionQuery = '"BANDID" = 'GRNN00A''
lyr.definitionQuery = ("BANDID" = 'GRNN00A')
lyr.definitionQuery = ("BANDID" = \'GRNN00A\')
What am I doing wrong?
Solved! Go to Solution.
Your code is failing because the mxd variable isn't a map document object but a unicode string, which generates the unicode error you are seeing. Try this:
import arcpy
import os
#define the mxd from user input
mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(0))
#set the dataframe as the default "Layers"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#for every layer, query only those records where "BANDID" equals 'GRNN00A'
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
lyr.definitionQuery = "BANDID = 'GRNN00A'"
mxd.save()
del mxd
this is my script if you don't want an attachment for it:
import arcpy
import os
#get user to input the mxd into the script tool
mxd = arcpy.GetParameterAsText(0)
#define the mxd from the above
arcpy.mapping.MapDocument(mxd)
#set the dataframe as the default "Layers"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#for every layer, query only those records where "BANDID" equals 'GRNN00A'
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
lyr.definitionQuery = '"BANDID" = \'GRNN00A\''
mxd.save()
del mxd
Did you try:
lyr.definitionQuery = "BANDID = 'GRNN00A'"
Often the field name is not quoted; it depends on the geodatabase type/server.
Just tried it now, but it failed These are just shapefiles, no geodatabase or anything.
import arcpy
import os
#get user to input the mxd into the script tool
mxd = arcpy.GetParameterAsText(0)
#define the mxd from the above
arcpy.mapping.MapDocument(mxd)
#set the dataframe as the default "Layers"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#for every layer, query only those records where "BANDID" equals 'GRNN00A'
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
lyr.definitionQuery = "BANDID = 'GRNN00A'"
mxd.save()
del mxd
it's giving me this error message:
Traceback (most recent call last):
File "I:\For_Colin\arcModels\python_scripts\definition_query_bandID_GRNN00A.py", line 11, in <module>
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1496, in ListDataFrames
result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 745, in listDataFrames
return list(reversed([fr for fr in self.dataFrames if wildcardmatch(wildcard, fr.name)]))
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 710, in dataFrames
return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 694, in pageLayout
return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)
AttributeError: 'unicode' object has no attribute '_arc_object'
Failed to execute (DefineBandIDAsGRNN00A).
Your code is failing because the mxd variable isn't a map document object but a unicode string, which generates the unicode error you are seeing. Try this:
import arcpy
import os
#define the mxd from user input
mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(0))
#set the dataframe as the default "Layers"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#for every layer, query only those records where "BANDID" equals 'GRNN00A'
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
lyr.definitionQuery = "BANDID = 'GRNN00A'"
mxd.save()
del mxd
Thank you!!
Yes that worked.
Thanks Randy and Joshua, both of you contributed to this success
Please mark someone's response as the answer so this question is closed out.
What is the tool parameter data type set as for the mxd variable? Try adding a print statement to see what is being returned.
import arcpy
import os
#get user to input the mxd into the script tool
mxd = arcpy.GetParameterAsText(0)
#define the mxd from the above
arcpy.mapping.MapDocument(mxd)
arcpy.AddMessage("{}".format(mxd))
...
To ensure fields are being formatted properly in sql queries, you can use AddFieldDelimiters.