iQuery = ' "Name" = \'Joe Blogs\' ' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)
iPerson = ???Joe Blogs??? iQuery = '"Name" = "' + aCenterMapOn + '"' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)
Solved! Go to Solution.
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' >>>
# your code iQuery = ' "Name" = \'Joe Blogs\' ' print iQuery # modified code print "\"%s\" = '%s'" % ("Name", "Joe Blogs")
>>> arcpy.env.workspace = r'Database Connections\MCPA.sde' >>> inFC = 'MCPA.DBO.PARCEL_PUBLIC' >>> qryFieldName = arcpy.AddFieldDelimiters(inFC, 'RECHAR') >>> print qryFieldName "RECHAR" >>> qry = qryFieldName + " = '" + aCenterMapOn + "'" >>> print qry "RECHAR" = '00166972-004500' (syntax): >>> # SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause}) >>> # MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info}) >>> >>> arcpy.MakeFeatureLayer_management(inFC, 'lyr', qry) <Result 'lyr'> (getting the count to verify the selection took place): >>> arcpy.GetCount_management('lyr').getOutput(0) u'1' >>>
Sorry, you've totally lost me!
>>> qry2 = "%s = '%s'" % (qryFieldName, aCenterMapOn) >>> print qry2 "RECHAR" = '00166972-004500'