Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reorder my primary key rows using Java/Android and SQLite DB?
    text
    copied!<p>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.</p> <p>For example, my DB after deleting some rows looks like this: --------1-------------Question1-------------Answer1---------0 --------3-------------Question2-------------Answer2---------3 --------5-------------Question3-------------Answer3---------5</p> <p>After exiting my activity, I want to look like this: --------1-------------Question1-------------Answer1---------0 --------2-------------Question2-------------Answer2---------3 --------3-------------Question3-------------Answer3---------5</p> <p>Tried onDestroy() function using this</p> <blockquote> <p>@Override</p> <p>protected void onDestroy() {</p> <pre><code> super.onDestroy(); if (mDbHelper != null) { for(int i=0; i&lt;mDbHelper.countRows(); i++){ try{ mDbHelper.updateDate(Integer.toString(i++), ggQuestions[i], ggAnswers[i], pozz++); }catch(Exception ex){} } mDbHelper.close(); } </code></pre> <p>}</p> </blockquote> <p>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:</p> <blockquote> <pre><code> mDb.execSQL("UPDATE flagtable SET _id="+_superID+", answer='"+answ+"', position="+ Integer.toString(pozz)+" WHERE question='"+quest+"'"); </code></pre> </blockquote> <p>I get this error: Failure 19 primary key must be unique when executing UPDATE</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload