jeudi 28 janvier 2016

Update ID field in SQLite database when an entry is removed

I have a SQLite database in my android application which holds multiple username/password entries, this content is placed into a ListView.

What I would like to achieve, is when an item is longpressed, it is removed from the list, but I'm encountering an issue.

I use the following code to remove an entry from the database:

    public void deleteEntry(int ID) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_LOGINS, KEY_ID + " = ?",
            new String[]{Integer.toString(ID)});
    db.close();
    }

The data is stored in the database in the following manner:

--------------------------
1|server|username|password
2|server|username|password
3|server|username|password
4|server|username|password
--------------------------

Now if I was to mark entry #2 for deletion, I would end up with the following

--------------------------
1|server|username|password
3|server|username|password
4|server|username|password
--------------------------

Which causes issues attempting to access the other entries, as I do all my lookups using the first field. Using the position from my OnItemLongClickListener, if I was to press the 2nd item in the list, it would be searching for an item with ID of 2.

How can I update the entries in my database so that my ID field will be sequentially numbered? Is there a better alternative to using an ID field?

Aucun commentaire:

Enregistrer un commentaire