0
I’ve researched a lot in the inter but found no solution...
For example, I have an aptitude test. Hence I create a table: "Bdteste.db", with a bank: "candidates", with the fields: _id, name and notes.
I launch all the candidates first and only after the test, do I launch your grades.
I order the bank by the largest the smallest notes (ORDER BY DESC notes). But from a test with 20 candidates, I want to delete the last 15 grades, and take only the top 5.
So I did:
try {
SQLiteDatabase dc = openOrCreateDatabase("DBTeste.db", MODE_PRIVATE, null);
Cursor res = dc.rawQuery("SELECT * FROM candidatos ORDER BY notas DESC ", null);
// Criei esta rotina abaixo para apagar os últimos registros, funciona, mas ela é muito grande
int cont = 0, id = 0;
while (!res.isAfterLast()) {
cont++;
res.moveToNext();
if (cont > 15) {
id = Integer.valueOf(res.getString(res.getColumnIndex("_id")));
dc.execSQL("DELETE FROM candidatos WHERE _id = '" + String.valueOf(id) + "' ");
}
}
res.close();
dc.close();
} catch (Exception e) {
e.printStackTrace();
}
Can you delete this using a single line? For example, it could be like this:
dc.execSQL("DELETE FROM candidatos WHERE linhas > 15 ");
that is, delete the last 15 records, which are the last 15 rows of the database...
I appreciate some help...
Only evaluate what the tiebreaker will be if there are multiple candidates with a tie on the highest score to be eliminated.
– anonimo
@anonimo It is a good question that AP will have to evaluate/consider.
– ramaral
ramaral, you’re a genius. It worked. On the ... tie-breaker... no need because the above doubt is an example. The real bank deals with records in a game. However I still caught a little, because I did not want to delete even the stick. Only deleted after I closed the cursor ( dc.close and res.close ), so I ask, it was not to delete even without closing...
– Adonis
"...it was not to delete even without closing...", depends on whether the cursor "puts" a lock or not.
– ramaral
ramaral, could explain better?
– Adonis
No further information, no.
– ramaral
Sorry everyone, I had not noticed before about the need to mark: "This comment adds something useful to the post". I marked now.
– Adonis