从Android数据库获取价值

时间:2018-07-29 12:01:27

标签: android android-database

我创建了一个database并将得分值存储到database中,但是我想retrieve得分值,但似乎无法做到这一点。我曾尝试研究寻找答案,但似乎找不到正确的解决方案。我想从database检索得分值。

插入分数

TrackerDb.InsertOrUpdateScore(this, score, Screen1_Type_Of_Problem.Screen.SCORE);

insertOrUpdateScore

public static void insertOrUpdateScore(Context context, int score, int activity) {
    TrackerDb.DBHelper dbHelper = new TrackerDb.DBHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String[] projection = {ID, TIME, TYPE, SCORE};
    String where = TYPE + " = ?";
    String[] whereArgs = {String.valueOf(activity)};
    String orderBy = TIME + " DESC";

    Cursor cursor = db.query(TABLE_NAME, projection, where, whereArgs, null, null, orderBy);
    boolean sameDay = false;

    Date currentTime = Calendar.getInstance().getTime();
    int StoredPoint = 0;
    long lastStored = 0;

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            lastStored = cursor.getLong(cursor.getColumnIndex(TIME));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            sameDay = (sdf.format(new Date(lastStored))).equals(sdf.format(currentTime));
            if (sameDay) StoredPoint = cursor.getInt(cursor.getColumnIndex(SCORE));

        }
        cursor.close();

    }


    ContentValues cv = new ContentValues();
    cv.put(SCORE, score + StoredPoint);

    if (sameDay) {
        db.update(TABLE_NAME, cv, TIME + " = ?", new String[]{String.valueOf(lastStored)});
    } else {
        cv.put(TYPE, activity);
        cv.put(TIME, currentTime.getTime());
        cv.put(SCORE, score);
        db.insert(TABLE_NAME, null, cv);
    }


}

getStoredItem

public static Cursor getStoredItems(Context context) {
    DBHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String[] projection = {ID, TIME, TYPE, DATE, SCREEN, RATING, SCORE};
    String orderBy = ID + " DESC";
    Cursor cursor = db.query(TABLE_NAME, projection, null, null, null, null, orderBy);
    return cursor;
}

0 个答案:

没有答案