I have a SQLite db for my highscores table. Currently I am having trouble checking if the new score makes the highscores and also sorting the highscores table.
When the game is over, Results.java is called.
Results.java
total_score = dh.calculateTotalScore(score, percentage);
low_score = dh.check(score, percentage, total_score);
if(total_score > low_score) {
dh.delete(10);
dh.insert(score, percentage, total_score);
} else {
dh.insert(score, percentage, 9999999);
}
dh.sort();
All the methods being called in Results.java are coming from DatabaseHelper.java.
DatabaseHelper.java
public void sort() {
db.rawQuery("SELECT * FROM " + DB_TABLE + " ORDER BY " + TOTAL_SCORE, null);
}
public long calculateTotalScore(long score, int percentage) {
long i;
return i = (percentage * 1000) + score;
}
public long check(long score, int percentage, long sum) {
Cursor c = db.rawQuery("SELECT " + TOTAL_SCORE + " FROM " + DB_TABLE, null);
long count = c.getCount();
long low_score;
if(count == 10) {
c.moveToLast();
low_score = c.getInt(c.getColumnIndex(TOTAL_SCORE));
return low_score;
} else {
return count;
}
}
public long insert(long score, int percentage, long total_score) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
values.put(TOTAL_SCORE, total_score);
return db.insert(DB_TABLE, null, values);
}
public void delete(int row) {
db.delete(DB_TABLE, RANK + "=" + row, null);
}
The output for TOTAL_SCORE is being displayed as follows:
- 1
- 2
- 40851
- 1
- 2
- 40804
- 60811
- 60811
- 50816
What I desire is for the output to be in numerical order. Like this:
- 1
- 1
- 2
- 2
- 40804
- 40851
- 50816
- 60811
- 60811
The order they are in above is (I think) just the order they were inserted into the db. No errors happen when the program runs and the program does not crash. More code can be provided if needed.
order byclause in yoursql. If you are concerned how many rows you want you can also uselimitclause with yoursql.and also if you are concerned about duplicate result then you can usegroup by