1

I am defining a function for use in a ArcGIS tool that will verify attributes, catch errors, and obtain user input to rectify those error. I want the tool to select and zoom to the segment that is being currently assessed so that they can make an informed decision. This is what I have been using, and it works well. But the CONVWGID is the variable that will be changing, and I'm not sure how to input that variable into an SQL statement without causing errors.

This is how I had tested the logic:

def selectzoom():
    arcpy.SelectLayerByAttribute_management(Convwks, "NEW_SELECTION", " [CONVWGID] = 10000001")

    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
    df.zoomToSelectedFeatures()
    arcpy.RefreshActiveView()

Then I needed to work the variable into the function in order to accept different CONVWGID values, which gives me a Runtime/TypeError that I should have known would happen.

Runtime error - Traceback (most recent call last): - File "string", line 1, in module - TypeError: cannot concatenate 'str' and 'int' objects

def selectzoom(convwkgid):
    delimfield = '" [CONVWGID] = ' + convwkgid + ' "'
    arcpy.SelectLayerByAttribute_management(Convwks, "NEW_SELECTION", delimfield)

    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
    df.zoomToSelectedFeatures()
    arcpy.RefreshActiveView()

And when I alter the delimfield line to change the integer into a string, it selects all of the attributes in the entire feature class. Not just the one that had been passed via the function call.

delimfield = '"[CONVWGID] = ' + str(convwkgid) + '"'

I'm not amazing with SQL and maybe I'm missing something basic with this statement, but I can't figure out why it won't work when I'm basically giving it the same information:

"[CONVWGID] = 10000001"
'"[CONVWGID] = ' + str(convwkgid) + '"'

2 Answers 2

4

It's maybe your extra double quotes at the beginning and end of your code:

Let's say convwkgid = 10000001

'"[CONVWGID] = ' + str(convwkgid) + '"' doesn't equal "[CONVWGID] = 10000001"

'"[CONVWGID] = ' + str(convwkgid) + '"' would actually be '"[CONVWGID] = 10000001"'

Try instead:

'[CONVWGID] = ' + str(convwkgid)

5
  • Hey @Emil Brundage, This was the fix I needed. I don't know why I didn't try to troubleshoot it like that. Thanks a lot for your help! Commented Jul 13, 2015 at 19:00
  • Any time. Enjoy the pythoning. Commented Jul 13, 2015 at 19:01
  • As an aside the square brackets are not needed for SQL statements unless there's a join involved. '[CONVWGID] = ' + str(convwkgid) is the same as 'CONVWGID = ' + str(convwkgid). This simplifies the syntax some cases use "FIELD" where others use [FIELD] - neither matters so you don't need to test if it's a file/personal geodatabase, SDE, shapefile or CAD feature class before scripting your query. Commented Jul 14, 2015 at 2:40
  • @MichaelMiles-Stimson Hey, I actually never knew that before so thanks. I always thought that SQL statement syntax was very dependent on the file being processed/looked at, and just took whatever the query builder suggested to me. I'm gonna look into this more now! Commented Jul 14, 2015 at 14:36
  • Delimiting the field is required in CalculateField_management, have a look at AddFieldDelimeters resources.arcgis.com/en/help/main/10.1/index.html#//… which takes a lot of hard work out. Another 'special' is the Mod function FIELD MOD 10 = 0 or mod(FIELD,10) = 0 depends on the database... the more advanced SQL definitely depends on the database being queried. Commented Jul 14, 2015 at 21:42
3

Let arcpy build the correct syntax for you and use .format() so you don't need to juggle extra quotes:

def selectzoom(convwkgid):
    fc = "Convwks"
    field = arcpy.AddFieldDelimiters(fc, "CONVWGID")
    selection = "{f} = {v}".format(f=field, v=convwkgid)

    arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", selection)

    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
    df.zoomToSelectedFeatures()
    arcpy.RefreshActiveView()
2
  • This helped me to catch an error in my coding, and I never really utilize the .format function as often as I could. Thanks for the help, the reminder, and taking the time to answer! Commented Jul 14, 2015 at 14:38
  • I didn't know the .format() method took keywords {f}, {v} in addition to indices {0}, {1}. Thanks!
    – John
    Commented Jul 22, 2015 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.