I am developing an application where I build a database, I build my database with help of tutorial http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/, the code is working fine, but the problem is that when I delete an item from a particular id, it just removes the row and that id goes missing. How can I avoid loss of the that id
The code is given below:
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// This string is used to create the database. It should
// be changed to suit your needs.
String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
");";
// execute the query string to the database.
db.execSQL(newTableQueryString);
}
and for deletion
public void deleteRow(long rowID)
{
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
If you want to keep track of all the
IDs that were allocated at some point, you can’t delete the corresponding rows in your “primary” table.You could use an extra state column (int or boolean) to store whether that record is active or not.
When you create a new item, set its state to “active”. When you delete it, don’t delete the row but update the state column to “inactive”.