To fetch the number of SQLite results before iterating over sqlite3_step calls, first run a query on the count. Such as this statement to count the number of ‘x’ items in table. The first result column will contain the count.
SELECT count( index ) FROM table WHERE item=x