Reputation: 1858
I want to increase my integer primary key by 1 for every new entry in a specific table. I read that i could do that by using AUTOINCREMENT. On the other hand i found the note below:
Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. One is the usual increment. However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence. AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential.
So my question is, shall i use autoincrement to keep counting my rows in the table?
Upvotes: 0
Views: 163
Reputation: 1851
Use AUTOINCREMENT for increasing the primary key and a simple query to count the number of rows in the table. Don't use AUTOINCREMENT to count the number of rows.
SELECT COUNT(*) FROM table_name
Upvotes: 2
Reputation: 2253
Usually there's no need to be perfectionist with your db key index - its important that it will be unique but in 99% of the times the consistency is not important. AUTOINCREMENT ensure your key id will be unique and that's why you should use it.
of-course I don't know the reason you want a consistent key so there is a chance you do need it but there's other aids for anything you would think of doing with a consistent id row in Android - for counting and going through your table you can use cursors.
Upvotes: 0
Reputation: 6892
You should count the number of rows using
SELECT Count(*) FROM tableName
For ensuring a unique id for your rows, you can use the autoIncrement function. That is its purpose and that is what it will do.
Upvotes: 1