Home > OS >  Android Room: How to auto generate primary id, without using "autoGenerate = true" (To avo
Android Room: How to auto generate primary id, without using "autoGenerate = true" (To avo

Time:02-11

Background

Just recently, I learn that using AUTOINCREMENT in SQLite is not encouraged.

enter image description here

CodePudding user response:

You don't need to achieve the same in Android room. In SQLite, a column with the type of INTEGER PRIMARY KEY is an alias for the ROWID, except if you specify WITHOUT ROWID.

So, upon insert your id will receive an unused integer unless specified otherwise.

The reason for not recommending the AUTOINCREMENT keyword is that unless it's a PRIMARY KEY, then it is unlikely necessary to accept the costs in terms of CPU, memory usage, disk space and I/O overhead. However, using a PRIMARY KEY is in fact a good idea. Note that the page you have shared never claims you should not use a PRIMARY KEY, it just discourages the usage of the AUTOINCREMENT keyword.

  • Related