1

I have a first activity where I create a database and insert some data. In my second activity I remove datas from my DB and the thing that I would like to know is how to reorder my row ID-s to be in consecutive order.

For example, my DB after deleting some rows looks like this: --------1-------------Question1-------------Answer1---------0 --------3-------------Question2-------------Answer2---------3 --------5-------------Question3-------------Answer3---------5

After exiting my activity, I want to look like this: --------1-------------Question1-------------Answer1---------0 --------2-------------Question2-------------Answer2---------3 --------3-------------Question3-------------Answer3---------5

Tried onDestroy() function using this

@Override

protected void onDestroy() {

  super.onDestroy();

  if (mDbHelper != null) {
      for(int i=0; i<mDbHelper.countRows(); i++){
          try{
              mDbHelper.updateDate(Integer.toString(i++), ggQuestions[i], ggAnswers[i], pozz++);
          }catch(Exception ex){}
      }

      mDbHelper.close();
  }

}

where mDbHelper is my Database, my primary key is String (that's why using integer to string) and my updateDate sql function looks like this:

  mDb.execSQL("UPDATE flagtable SET _id="+_superID+", answer='"+answ+"', position="+
              Integer.toString(pozz)+" WHERE question='"+quest+"'");

I get this error: Failure 19 primary key must be unique when executing UPDATE

4
  • Solved by deleting all records from database and than re-inserting them...this is awful because I had to save them in seperate arrays, for bigger databases wouldn't be indicated. Anyone has a more simple method?
    – Radu Dan
    Aug 20, 2011 at 13:34
  • I don't think you can other than with a new table, but I upvoted because I would like to know also. I do know that inside your database, there is a table sqlite_sequence. I may play around with that after work.
    – Mobius
    Aug 20, 2011 at 14:13
  • Why do they need to be consecutive? Not generally done. If you have the need for some kind of surrogate consecutive order, I'd probably do it with a second column that can be updated in-place without changing the primary key of every row (and having to update any references to those primary keys in other tables...)
    – Joe
    Aug 20, 2011 at 14:49
  • I need them to be in consecutive order, because after exiting my second activity I enter my first activity and do more inserting...
    – Radu Dan
    Aug 21, 2011 at 14:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.