0

My script runs perfectly for feature classes, but not shapefiles. It crashes at the point of selection, provided there is a query.

gp.Workspace = "C:/someworkspace"
fc = "featureclass.shp"
query = "[SOME_TEXT_FIELD] = \"666\""
gp.MakeFeatureLayer(fc, "flyr")
gp.SelectLayerByAttribute("flyr", "NEW_SELECTION", query)

returns

ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

Tested a similar simple example in the Python Shell. This runs fine with a geodatabase and feature class. I assume there is some sort of special treatment for shapefiles I'm not aware of, but arcgis desktop help does not specify anything.

marked as duplicate by PolyGeo, Fezter, Jason Scheirer, whuber Dec 12 '13 at 7:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Try using single-quotes around the target value in your SQL query: query = "[SOME_TEXT_FIELD] = '666'" – Jason Dec 11 '13 at 20:56
  • Did try that, both with and without escape characters. I don't think the query should behave any differently. I thought maybe I could create the feature layer without the file extension, but that doesn't work. My ArcMap has been behaving a little strangely (and slowly) at times since the connection for my ArcInfo license on the server has been a bit intermittent today, so I wondered if that that had anything to do with it. – Sleep6 Dec 11 '13 at 20:59
  • 1
    Ok, have you tried using quotation marks instead of brackets around the field? query = "\"SOME_TEXT_FIELD\" = '666'" – Jason Dec 11 '13 at 21:04
  • More specific than that -- it works with query = "\"FIELD\" = \'121\'". I'll need an if statement for this I suppose. Thank you for the help. – Sleep6 Dec 11 '13 at 21:30
2

This, and only this, works - query = "\"FIELD\" = \'121\'". Let it be known that gp is much picker with shapefiles than feature classes when it comes to SQL query.

1

If you do not like using the escape character '\' then this should work too:

query = '"FIELD" = ' + "'121'"

This ArcPy help page (that applys equally to arcgisscripting) mentions that:

The field delimiters used in a SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotes (" "), personal geodatabases use square brackets ([ ]), and ArcSDE geodatabases don't use field delimiters

  • Yes, though I had difficulty finding that in 9.3. help for gp. I actually do prefer the escape character since it's slightly faster to run in python than using +. – Sleep6 Dec 12 '13 at 14:27

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