Select to view content in your preferred language

Select by attribute using get parameter as text with Enterprise Geodatabase.

388
3
01-29-2025 03:38 PM
ColeNelson
Emerging Contributor
import arcpy

arcpy.env.workspace = r"C:\PPCAnalysis\PPCAnalyis\PPCAnalyis.gdb"
select_stations = arcpy.GetParameterAsText(0).split(";")
firestations = r"C:\PPCAnalysis\PPCAnalyis\ISRB_DATA.sde\DBO.FireStations"

qry = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(datasource= firestations, field = 'STATIONID'), ', '.join(select_stations))
selected_stations = arcpy.management.SelectLayerByAttribute(in_layer_or_view= firestations, where_clause=qry)

arcpy.SelectLayerByAttribute_management(in_layer_or_view=firestations, where_clause= qry)

I am trying to develop a custom tool where the user can enter "STATIONID" for a fire station, and it will select it. 

The fire station feature class is being stored in an Enterprise Geodatabase, and when I enter the parameter (see screenshot), it says it was successful.  However, when I open the attribute table, nothing is actually being selected.  Also, neither is the point on the map.

Screenshot 2025-01-29 163549.png

For future development reasons I do want the selection to be made directly from the enterprise geodatabase.  Thanks so much for the help😁.

0 Kudos
3 Replies
AlfredBaldenweck
MVP Regular Contributor

My bet is twofold: 1) your parameter is actually text, in which case if your field is a number field, the query will run just fine but not select anything. Change the parameter type to Long (integer) if it isn't already, and/or 2) having made the parameter an integer, use GetParameter(0) instead, which will return the true value instead of a string.

alternatively, you can pop an int() around line 4, if you trust your users to input numbers.

0 Kudos
RhettZufelt
MVP Notable Contributor

Suspect the issue is with the .split(";") being incorporated into the qry statement.

What happens if you put a print(qry) line in between lines 7  & 8?

Didn't test with GetParameter..., but if I set the Station = '276':

 

>>> select_stations = "276"
>>> qry = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(datasource= firestations, field = 'STATIONID'), ', '.join(select_stations))
>>> print(qry)
STATIONID IN (2, 7, 6)

 

Then  you can make the modifications to get the proper query statement.

R_

 

 

0 Kudos
DavidSolari
MVP Regular Contributor

Does the tool work better if you add that feature class to a map as a layer, then use the layer specifically? I've found using Select By Attribute/Location on feature class paths is flaky.