我的数据库保留前 10 条记录。db为空时,db记录分数,一路正确插入,直到有10条记录。然后,例如,当 db 有 10 个分数,并且用户得到一个应该是新的 #1 分数的分数时,它只是用新的分数替换当前的 #1 分数,而不是插入并将其下方的所有内容向下颠倒一个并且删除最低分数。希望我解释得很好,但如果不只是评论,我会尝试进一步澄清。
我的问题是我怎样才能让高分正确执行,因为当高分已经满了 10 条记录时,一条新记录被插入到正确的位置,而它下面的其余部分全部向下移动,最后一条记录得到完全撞倒了(因为它是新的#11)。
数据库助手.java
public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
private static final int DATABASE_VERSION = 6;
private static final String DB_NAME = "test3";
private static final String DB_PATH = "/data/data/matt.lyons.bibletrivia/databases/";
private static final String TABLE = "HighscoresList";
// Table columns names.
private static final String RANK = "_id";
private static final String SCORE = "score";
private static final String PERCENTAGE = "percentage";
private static final String TOTAL_SCORE = "total_score";
private static final String CATEGORY = "category";
//Constructor for a DatabaseHelper.
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}
//Open the DB so it is editable.
public SQLiteDatabase openDB() {
db = this.getWritableDatabase();
return db;
}
//Delete a selected one row.
public void delete(long lowScore) {
lowScore = getLowest();
db.delete(TABLE, TOTAL_SCORE + "=" + lowScore, null);
}
//Sort rows in order of the TOTAL_SCORE column.
public long getLowest() {
Cursor c = db.rawQuery("SELECT * FROM " + TABLE + " ORDER BY " + TOTAL_SCORE, null);
long count = c.getCount();
long lowScore = -1;
if(count == 10) {
c.moveToLast();
lowScore = c.getInt(c.getColumnIndex(TOTAL_SCORE));
}
return lowScore;
}
//Calculate the TOTAL_SCORE based on the SCORE and PERCENTAGE of a game.
public long calculateTotalScore(long score, int percentage) {
long i;
return i = (percentage * 1000) + score;
}
//Check if new record makes the top 10.
public long check(long score, int percentage, long sum) {
Cursor c = db.rawQuery("SELECT " + TOTAL_SCORE + " FROM " + 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;
}
}
//Insert new record.
public long insert(long score, int percentage, long total_score, String category) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
values.put(TOTAL_SCORE, total_score);
values.put(CATEGORY, category);
return db.insert(TABLE, null, values);
}
//Create the table and all columns.
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " ("
+ RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ SCORE + " LONG,"
+ PERCENTAGE + " INTEGER,"
+ CATEGORY + " STRING,"
+ TOTAL_SCORE + " LONG"
+ ");");
}
}
结果.java
public class Results extends Activity {
DatabaseHelper dh;
public void onCreate(Bundle savedInstanceState) {
dh = new DatabaseHelper(this);
dh.openDB();
showResults();
}
public void showResults() {
total_score = dh.calculateTotalScore(score, percentage);
if(dh.getLowest() == -1) {
dh.insert(score, percentage, total_score, category);
} else {
dh.delete(dh.getLowest());
dh.insert(score, percentage, total_score, category);
}
}
}