Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite Database After delete table, will the id starts from 1?

I have a doubt that if i delete the table using following statements,

     SQLiteDatabase db = this.getWritableDatabase();
     db.delete(date_difference, null, null);

then if i'm inserting a row as a fresh and first record into the table, will the id primary key auto increment of the records starts from 1 ?

like image 669
Pattabi Raman Avatar asked Dec 10 '12 09:12

Pattabi Raman


2 Answers

If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically. The usual algorithm is to give the newly created row a ROWID that is one larger than the largest ROWID in the table prior to the insert. If the table is initially empty, then a ROWID of 1 is used. If the largest ROWID is equal to the largest possible integer (9223372036854775807) then the database engine starts picking positive candidate ROWIDs at random until it finds one that is not previously used.

So yes, after you delete the table, IDs will start from 1

http://www.sqlite.org/autoinc.html

like image 187
Korniltsev Anatoly Avatar answered Nov 11 '22 12:11

Korniltsev Anatoly


The documentation provided states that that delete method is a:

Convenience method for deleting rows in the database.

The syntax is:

int delete(String table, String whereClause, String[] whereArgs)

Therefore it won't start from 1 again. It'll continue on from the last increment. If you deleted the whole table, then re-created it, the increment would begin at 1.

like image 23
alistair Avatar answered Nov 11 '22 11:11

alistair