Select to view content in your preferred language

Python value variable in SQL expression FeatureLayer.query(where = "attributeName = 'StringValue'" )

1625
5
Jump to solution
09-13-2022 03:11 AM
AngelRomoSandoval
Regular Contributor

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

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Frequent Contributor

Hi @AngelRomoSandoval 

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)
~ learn.finaldraftmapping.com

View solution in original post

5 Replies
Clubdebambos
Frequent Contributor

Hi @AngelRomoSandoval 

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)
~ learn.finaldraftmapping.com
AngelRomoSandoval
Regular Contributor

Yes, I access with the Item Id.  Thank you for all the examples!

DavidAnderson_1701
Frequent Contributor

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.

0 Kudos
AngelRomoSandoval
Regular Contributor

Thank you for the info, no, it comes not from an external source. I will keep it in mind.

0 Kudos
KimOllivier
Honored Contributor

There is already a function to validate the SQL string built in to the API. Very straightforward.

https://developers.arcgis.com/python/api-reference/arcgis.features.html#arcgis.features.FeatureLayer...