SQLite Drop and Recreate Table

Pztar picture Pztar · Jan 22, 2013 · Viewed 38.3k times · Source

How do I drop and recreate a table with updated information?

For example; I have a table in my application with 5 records.

ID     Entry
1      a
2      b
3      c
4      d
5      e

Now when I delete a record lets say, ID 3 the table becomes

 ID     Entry
1      a
2      b
4      d
5      e

How can I drop it, and recreate it to become this:

  ID     Entry
1      a
2      b
3      d
4      e

Actually the deletion is controlled by the user so any record can be deleted at any time.

Answer

Opiatefuchs picture Opiatefuchs · Jan 22, 2013

You could do following: delete the row You want to delete:

     public boolean deleteRow(int id) 
      {
         return db.delete(YOUR_TABLE, ROW_ID + "=" + _id, null) > 0;
       }

read remaining entries (only entries, not the id) into an ArrayList, drop Your table:

        db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE);

and recreate Your table. Then the ID Numbers will be again from 1-XX in Order