すべての if-elseif-else ループと while ループで頭が回転するメソッドを実装しようとしています。自動インクリメント、スコア(ロング)、パーセンテージ(int)の3つの列を持つSQLiteデータベースがあります。パーセンテージは 10 問の正しさから得られ、スコアは 0 から 1000 の間で計算される数値になります。したがって、パーセンテージには多くの同点があり、(おそらく) スコアには何もありません。
ハイ スコアは最初にパーセンテージでランク付けされ、次に同じパーセンテージ値を持つすべてのレコードの「タイを破る」ために、スコアでランク付けされます。それはすべて意味がありましたか?そうでない場合は、お知らせください。編集して、より適切に説明できるようにします。
私の質問は、誰かがこの方法を実行するために従うことができる提案や他の例を持っているかどうかです. また、私のやり方で大丈夫ですか?ほとんどの場合、特定のインデックスでデータベースに挿入する必要があるためrawQuery()
、メソッドを使用する代わりにテーブルに挿入しています。insert()
私の挿入方法も含めました。
//Check if new record makes the top 10.
public int check(long score, int percentage) {
Cursor c1 = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE, null);
Cursor c2 = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE, null);
if(c1 != null && c2 != null) {
int i = 0;
while(c1.moveToNext() == true) {
int ii = 0;
c1.moveToPosition(i);
do {
int x = c1.getInt(c1.getColumnIndex("PERCENTAGE"));
if(x > percentage) {
db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
} else if (x < percentage ) {
i++;
} else {
int y = c2.getInt(c2.getColumnIndex("SCORE"));
while(x == percentage) {
//ahhhh!
}
if(y > score) {
db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
} else
if(c2.getCount() == 10) {
//Delete last record in high score and insert at index.
delete(10);
//db.rawQuery("DELETE FROM " + TABLE + " WHERE " + RANK + " = 10;", null);
db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
return true;
} else {
//No deletion - just insert.
db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
return true;
}
} else {
return false;
}
} while (c2.moveToNext());
} else {
return false;
}
} else {
return false;
}
}
入れる()
//Insert new record.
public long insert(long score, int percentage) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
return db.insert(TABLE, null, values);
}