Search code examples
androidsqliterowid

android sqllite database rowid increment while insertion


I have a table in my database with one field declared as

_id integer primary key

There is no AUTOINCREMENT.

Here i have a doubt about how rowid will be updated during insert after a delete. what i observed is

  1. if the last row id is x and it is deleted, the next row inserted gets id x.

But if i delete some other rows with id < x and then insert a new row the rowid of the newly inserted row is x+1 .

Please confirm is my observation right ( rowid reused only if the last rows are deleted and not in between rows. )

I saw the explanation for AUTOINCREMENT. I want to know the behaviour when no AUTOINCREMENT is set


Solution

  • The AUTOINCREMENT keyword is not required for SQLite tables to generate primary key values automatically.. what ever you add to the database will be added as the last row...so if u delete last row(x).. new element will be added at x position.. if you delete from middle or not.. new element on addition will go to x+1 position...

    on using auto increment.. if you delete last element at x position next id x+1 will be generated by autoincrement algorithm.. makin the next addition at x+1 position..