我的高分表有一个 SQLite 数据库。目前,我在检查新分数是否获得高分以及对高分表进行排序时遇到问题。
当游戏结束时,Results.java 被调用。
结果.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();
所有被调用的方法Results.java
都来自DatabaseHelper.java
.
数据库助手.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);
}
TOTAL_SCORE 的输出显示如下:
- 1
- 2
- 40851
- 1
- 2
- 40804
- 60811
- 60811
- 50816
我希望输出按数字顺序排列。像这样:
- 1
- 1
- 2
- 2
- 40804
- 40851
- 50816
- 60811
- 60811
它们在上面的顺序(我认为)只是它们插入数据库的顺序。程序运行时不会发生错误并且程序不会崩溃。如果需要,可以提供更多代码。