Search code examples
androidsqliterecordnotnull

Get total number of not null records in Android SQLite database


I want to get the number of NOT NULL records from my SQLite database. Since I'm using autoincrement, the last inserted row won't give me the real number of records in the table as even if I delete any middle record, it'll insert at the position higher than the last inserted record.

The insert statement returns me the number of last inserted row, but I want this value on the fly.


Solution

  • Doing a count before on the table should work. Simply query for the id column with the where check of NOT NULL and on the returned cursor just call the getCount()

    Just to be sure: You should never ever, really never ever, manipulate the auto increment in a productive database. If you delete a record, than the "gap" should stay there. It has no impact on performance or anything else. If you insert a new record in the gap, you can create a lot of trouble...