jeudi 9 juillet 2015

Android update all records SQLite

a table of my database has only 7 records. A field contains integer values in sequence, from 1 to 7 (1,2,3,4,5,6,7). When I delete a record, for example, one that contains the number 5, I would like to renumber all remaining, so in this case the numbering should be (1,2,3,4,5,6), with the code created by me, unfortunately are the sequence (6,6,6,6,6,6). What is wrong?

//first, delete the particular record
  db.delete(OperatoriTable.TABLE_NAME, OperatoriTable.NUMERAZIONE + "= ?", new String[]{i.getStringExtra("NUM")});
//then, account records, and insert the new numbers
int numerazione_op = 0;
    String sql_numerazione = "SELECT COUNT(numerazione) FROM operatori";
         Cursor cur1 = db.rawQuery(sql_numerazione, null);
         while (cur1.moveToNext()) {
                numerazione_op = cur1.getInt(0);
                }
                cur1.close();
                ContentValues cv = new ContentValues();
                 for(int i = 1; i<=numerazione_op; i++){
                        cv.put(OperatoriTable.NUMERAZIONE, i);
                        db.update(OperatoriTable.TABLE_NAME, cv, null, null);
                    }

                }
            }
            c.close();
            db.close();
...
...
}

Aucun commentaire:

Enregistrer un commentaire