I use the following code in a layer action:

<uri definition etc>    
sql = 'id = "[% "id" %]"';
layername = 'TableName [% "id" %]';
vlayer = QgsVectorLayer(uri.uri(),layername,'spatialite');
<some code> if (vlayer.isValid() and vlayer.featureCount() > 0) else <some code>

The SQL-query gives as result 0 or more features. vlayer.featureCount() returns 0 or more as expected in QGIS 1.8, but always 359 in QGIS 2.0 (total features in Spatialite table with name TableName), so my code breaks in QGIS 2.0. When the SQL-query has no result and I add vlayer to the interface and turn Show Feature Count on the layers panel shows two counts (?!) [359] and [0]. When the query returns 1 feature, these counts are [359] and 1. Open Attribute Table shows 0 and 1 record which is all fine. So the SQL-query seems to run fine, but featureCount holds the wrong number in my opinion.

Show Feature Xount

Any clue?

  • sql = 'id = "[% "id" %]"'; that isn't valid SQL. What provider are you using this against? – Nathan W Oct 1 '13 at 21:12
  • @NathanW I think the SQL runs fine, but something is wrong with feautureCount. I've added some extra info to my question. – spingerfitz Oct 2 '13 at 16:24
  • I don't see how that query would run. The "[% "id" %]" is QGIS expression syntax, each provider uses it's own SQL style to query the layer. If the query isn't running correctly it might be returning a bad result. – Nathan W Oct 2 '13 at 22:52

Your Answer

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

Browse other questions tagged or ask your own question.