簡體   English   中英

SQLite DB高分表無法正確插入和排序-Android

[英]SQLite db high scores table not inserting and sorting correctly - Android

我的數據庫保留前10條記錄。 當數據庫為空時,數據庫會記錄分數並將其正確插入,直到有10條記錄為止。 然后,例如,當數據庫有10個分數,並且用戶獲得的分數應該是新的#1分數時,它只是將新的#1分數替換為新的#1分數,而不是將其下方的所有內容插入並顛簸到下面,然后刪除最低得分。 希望我能很好地解釋這一點,但如果不能只是評論,我會嘗試進一步澄清。

我的問題是,如何使高分正確執行,就像高分已滿並包含10條記錄,將新記錄插入正確的位置,然后將其下的其余所有位置向下移動,最后一條記錄完全取消(因為它是新的#11)。

DatabaseHelper.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);
        }
    }
}

為什么不只是:

"SELECT LIMIT 10 * FROM " + TABLE + " ORDER BY TOTAL_SCORE DESC"

這樣一來,您不必一直插入/刪除即可獲得前十名的成績嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM