1

I want to filter a layer with a variable and a LIKE condition.

The filter attribute is a timestamp column of Spatialite table. The table has also a join.

I successfully can filter this vector layer with:

layer_subset = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"acq_dat" LIKE \'27.06.2017%\'') )

The 27.06.2017 is the part of the timestamp string 27.06.2017 11:11:11.

Currently I'm writing a small plugin for exporting Shapefiles based on different dates. With a combo box the user can choose a date und this will be the variable for the filter.

I tried this:

layer_subset= layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"acq_dat" LIKE \'%s%\'' % acq_dat ) )

But I get the error:

Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: not enough arguments for format string

How can I set a filter variable with a LIKE condition?

1

Sometimes you have to post a question here to get the idea:

layer_subset = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"acq_dat" LIKE \'%s%s\'' % (acq_dat, '%')) )

Setting the % against acq_dat and passing %s twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.