Select to view content in your preferred language

Little SQL query help please

1543
12
Jump to solution
12-19-2012 07:05 AM
JonPedder
Deactivated User
I always seem to get hung up on these simple strings.

Here again I don???t quite get the syntax for use in SelectLayerByAttribute_management

If I use the following with a field named ???Name??? the query works just fine

iQuery = ' "Name" = \'Joe Blogs\' ' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)


However when I try to use a variable I can never seem to get the syntax right

iPerson = ???Joe Blogs??? iQuery = '"Name" = "' + aCenterMapOn + '"' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)


Thanks
Tags (2)
0 Kudos
12 Replies
CarlSunderman
Frequent Contributor
I would try working your way through it. Maybe start simple to figure out where your error is coming from. Try using the python window and use actual feature class names then do again declaring variables and see where the issue is coming from. That's usually how I work my way through
0 Kudos
T__WayneWhitley
Honored Contributor
Okay, sorry for the delay - but see the below, what I did that works (employing the techniques already discussed above).
I used a gdb feature class that I added a 'blank' row to in order to introduce the null vals (so I know I should get results).  Comments in the code below (again, it's an IDLE session):
IDLE 2.6.5       >>> import arcpy >>> fc = r'C:\Documents and Settings\whitley-wayne\Desktop\stage.gdb\RightBankPoints'  # (make sure the fc exists) >>> if arcpy.Exists(fc):print 'true'   true  # (could have entered the qry here for MFL, but you wanted to demo SelectLayerByAtt, which I think requires a layer) # (...so created a lyr first to feed into SelectLayerByAtt, a step only for demo purposes...) >>> arcpy.MakeFeatureLayer_management(fc, 'lyr') <Result 'lyr'>  # (verifying there are features in the lyr): >>> arcpy.GetCount_management('lyr').getOutput(0) u'198'  # (setting up the field with the correct delimiters [double quotes]): >>> selFld = arcpy.AddFieldDelimiters(fc,'RiverCode')  # (just checking...) >>> print selFld "RiverCode"  # (setting up the qry string): >>> qry = "%s %s" % (selFld, 'is not Null')  # (looks fine, ready for launch...) >>> print qry "RiverCode" is not Null  # (commence launch, the execution we've been waiting for): >>> arcpy.SelectLayerByAttribute_management('lyr', 'NEW_SELECTION', qry) <Result 'lyr'>  # (checking the count -- I entered 1 null record, so the count should be at least 1 less than before): >>> arcpy.GetCount_management('lyr').getOutput(0) u'197' >>>  


And it is.  Mission accomplished, hope that helps you.
0 Kudos
JonPedder
Deactivated User
Perfect Wayne! I'll keep this little function in my toolbox from now on. Thanks again for all your help

Jon
0 Kudos