I use the following code in a layer action:
<uri definition etc> sql = 'id = "[% "id" %]"'; uri.setSql(sql); 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 (?!)  and . When the query returns 1 feature, these counts are  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.