Hello,
I am trying to insert a variable where ClientName is written:
Fl_Filtered = FeatureLayer.query(where = "client = 'clientName'")
I tried this without success:
querytxt = 'where = "KundeName = ' + "'" + ""'%s'"" %kundeNameWhereFilter + "'" + '"'
Print(querytxt)
Fl_Filtered = FeatureLayer.query(querytxt)
The print ist returns the right String but the query returns an Error code: 400
Anyone knows how could be the solution?
Thanks
Solved! Go to Solution.
I like to use the string format, there are various options, see here.
The example below uses numbered indexes.
## example 1
client_name = "My Clients Name"
Fl_Filtered = FeatureLayer.query(where = "client = '{0}'".format(client_name)
### DO SOMETHING WITH THE CLIENT
## example 2
client_list = ["Client 1", "Client 2", "Client 3"]
for client_name in client_list:
Fl_Filtered = FeatureLayer.query(where = "client = '{0}'".format(client_name)
### DO SOMETHING WITH EACH INDIVIDUAL CLIENT
also, how are you accessing the Feature Layer?
from arcgis import GIS
agol = GIS("home")
feature_service = agol.content.get("***ITEM_ID***")
feature_layer = feature_service.layers[0] # where 0 is the index of the first layer in the featire service
client_name = "My Clients Name"
Fl_Filtered = feature_layer.query(where = "client = '{0}'".format(client_name)
I like to use the string format, there are various options, see here.
The example below uses numbered indexes.
## example 1
client_name = "My Clients Name"
Fl_Filtered = FeatureLayer.query(where = "client = '{0}'".format(client_name)
### DO SOMETHING WITH THE CLIENT
## example 2
client_list = ["Client 1", "Client 2", "Client 3"]
for client_name in client_list:
Fl_Filtered = FeatureLayer.query(where = "client = '{0}'".format(client_name)
### DO SOMETHING WITH EACH INDIVIDUAL CLIENT
also, how are you accessing the Feature Layer?
from arcgis import GIS
agol = GIS("home")
feature_service = agol.content.get("***ITEM_ID***")
feature_layer = feature_service.layers[0] # where 0 is the index of the first layer in the featire service
client_name = "My Clients Name"
Fl_Filtered = feature_layer.query(where = "client = '{0}'".format(client_name)
Yes, I access with the Item Id. Thank you for all the examples!
Be careful when doing this if the variable value is coming from an outside source. A SQL injection attack could be done. Before deploying the simple solution read a bit more about SQL injection attacks and how to defend your code against them. Here is one source, https://realpython.com/prevent-python-sql-injection/, many others to consult.
Thank you for the info, no, it comes not from an external source. I will keep it in mind.
There is already a function to validate the SQL string built in to the API. Very straightforward.