I have created a script tool that allows users to select a single value from a pick list, which gets inserted into a query expression.
The pertinent part of my code is pretty simple:
Input = arcpy.GetParameterAsText(0)
arcpy.MakeQueryTable_management(inTable, "TabTemp", "", "", "", "FieldName = " + str(Input))
So if a user selects as input "Item1", then the resulting SQL expression is FieldName = Item1.
I am hoping to make the input a multi-value pick list, so that a user could potentially select one, or multiple values as Input. For example, if the user selects Item1, Item2 and Item3, then the SQL expression should look like:
FieldName = Item1 OR FieldName = Item2 OR Field Name = Item 3
I'm not quite sure how to get started with the python code to do this. Can anybody offer suggestions for creating the necessary code to compile the SQL expression from an unknown number of values obtained from a multi-value pick list?
Answer
If you are aiming for an interface such as below:
Then you need to set the parameter as shown below:
In the Filter property set it as Value List and type in all the possible values.
The actual code to create the query would be:
import arcpy
import string
def main():
x = arcpy.GetParameterAsText(0)
vals = string.split(x,";")
whereClause = "FieldName IN (" + string.join(vals,",") + ")"
arcpy.AddMessage(whereClause)
if __name__ == '__main__':
main()
No comments:
Post a Comment