我创建了一个database
并将得分值存储到database
中,但是我想retrieve
得分值,但似乎无法做到这一点。我曾尝试研究寻找答案,但似乎找不到正确的解决方案。我想从database
检索得分值。
TrackerDb.InsertOrUpdateScore(this, score, Screen1_Type_Of_Problem.Screen.SCORE);
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);
}
}
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;
}