Select to view content in your preferred language

Where Clause SQL syntax problem - Urgent Please Help

2991
10
04-10-2013 07:00 PM
ScottMacDonald
Deactivated User
Hi,

I am running a select layer by attribute and location query on a feature layer of points made from a shapefile.

I have been stuck for hours on the wording of it and would be grateful if some one would tell me how to do it because I have looked and tried many variations to no avail and there are so many suggestions but none seem to fit.

I want the code to be

"CRIME_INDE" <= 0.02 AND "UNVERSITY" = 1

but I can't get the right syntax in this case beacuse all the components are variables.  Please could anyone offer any suggestions, it's really urgent and preventing me from progressing and I'll be happy to post up the resuts that work.

Thanks
scottaidh


# import system modules
import arcpy, os, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb
cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
crimeField = arcpy.GetParameterAsText(4) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimefieldindex = arcpy.GetParameterAsText(5) # crime index is CRIME_INDE and is a string 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimefieldindex)
universityField = arcpy.GetParameterAsText(6) # universityField is fieldname 'UNIVERSITY' SQL expression
universityfieldindex = arcpy.GetParameterAsText(7) # universityfieldindex is the UNIVERSITY field and is string integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
outfile = arcpy.GetParameterAsText(15) # ootput


arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", '"' + whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex'')
arcpy.CopyFeatures_management(citiesL, outfile)
[\CODE}
Tags (2)
0 Kudos
10 Replies
ScottMacDonald
Deactivated User
Thank you all very much for taking the time to reply.

The line that worked in the end was a Field Delimiter where clause


crimeField = arcpy.GetParameterAsText(4) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimeval = arcpy.GetParameterAsText(5) # crime index is CRIME_INDE and is a Double 0.02
whereClause = "{0} <= {1}".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimeval)
[\CODE]

Previously, I had two extra ' single abbreviations around the where clause 0 and 1 which made the expression something like ""CRIME_INDE"" = "0.02" so watch out for them! 

Thanks again for the help.

Scottaidh
0 Kudos