Search code examples
pythonlistrangearcpy

List of Ranges Used in SQL Expression, Creates "Invalid SQL Statement" Error


I'm trying to use a list of ranges in a SQL expression within my Python script, but whenever I try and run it, it gives me the following error.

ExecuteError: ERROR 999999: Error executing function.
An invalid SQL statement was used.
An invalid SQL statement was used. [PatientVisitsGeocoded]
An invalid SQL statement was used. [SELECT ObjectID FROM PatientVisitsGeocoded WHERE USER_CenterID = 1001 AND USER_MonthsSinceFHOpening = (1, 3)]
Failed to execute (FeatureClassToFeatureClass)

However, when I use only one range, instead of a list of ranges, the script runs fine.

I had a hard time finding examples of lists containing ranges, and I developed the syntax for my list from the best answer on this page:

Python List with ranges

I don't know if the syntax for my list of ranges is just wrong or if I need to change the way my expression is constructed. Any advice/help would be much appreciated.

Here is my full code:

# Set environment settings
arcpy.env.workspace = r"C:\arcGIS_Shared\Python\CenterHeatMaps6.gdb"

#Declare variables
fc = 'Open_GoHealth_Centers'
fields = ['USER_market_id','USER_GoHealth_ID','USER_GoHealth_Center_Name','USER_Opening_Date', 'USER_MonthsSinceFHOpening']
fieldname = 'USER_market_id'

# Set Markets to loop through
markets = [1000]
# Set Years to loop through
years = [2014, 2015]
# Set Ranges to loop through
myranges = [(1,3),(1,4),(1,5)]

sqlclause = (None, 'Order By USER_market_id, USER_GoHealth_ID')

for market in markets:
    print (market)
    #Define WHERE clause statement
    whereclause = """{} = """.format(arcpy.AddFieldDelimiters(fc, fieldname)) + str(market)

    for year in years:
        print (year)
        for myrange in myranges:
            print (myrange)
            with arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=(None, 'ORDER BY USER_market_id, USER_GoHealth_ID')) as cursor:
                #Loop through each row established in cursor
                for row in (cursor):
                    # Set local variables for the FeatureClasstoFeatureClass tool
                    inFeatures = "PatientVisitsGeocoded"
                    outLocation = r"C:\arcGIS_Shared\Python\CenterHeatMaps6.gdb"
                    outFeatureClass = "PatientVisits{0}_{1}_{3}".format(row[0], row[2], myrange, year) 
                    delimitedfield = arcpy.AddFieldDelimiters(arcpy.env.workspace,"USER_CenterID")
                    expression = """{0} = {1} AND USER_MonthsSinceFHOpening = {2}""".format(delimitedfield, row[1], myrange) 
                    print (expression)

#                   Execute FeatureClassToFeatureClass tool
                    arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outLocation, outFeatureClass, expression)

#                   #Print Results
                    print(row[2])
                    print(year)
                    print(myrange)

Solution

  • It should be In (1, 3) not = (1, 3).

    If you want the range between the values 1 and 3 then use Between 1 and 3. In means "match one of these values", it isn't inclusive.