private void updateExistingRow(final SQLiteDatabase database,final long id,boolean bumpCount){
  String stringId=String.valueOf(id);
  database.beginTransaction();
  final Cursor cursor=database.query(SongPlayCountColumns.NAME,null,WHERE_ID_EQUALS,new String[]{stringId},null,null,null);
  if (cursor != null && cursor.moveToFirst()) {
    int lastUpdatedIndex=cursor.getColumnIndex(SongPlayCountColumns.LAST_UPDATED_WEEK_INDEX);
    int lastUpdatedWeek=cursor.getInt(lastUpdatedIndex);
    int weekDiff=mNumberOfWeeksSinceEpoch - lastUpdatedWeek;
    if (Math.abs(weekDiff) >= NUM_WEEKS) {
      deleteEntry(database,stringId);
      if (bumpCount) {
        createNewPlayedEntry(database,id);
      }
    }
 else     if (weekDiff != 0) {
      int[] playCounts=new int[NUM_WEEKS];
      if (weekDiff > 0) {
        for (int i=0; i < NUM_WEEKS - weekDiff; i++) {
          playCounts[i + weekDiff]=cursor.getInt(getColumnIndexForWeek(i));
        }
      }
 else       if (weekDiff < 0) {
        for (int i=0; i < NUM_WEEKS + weekDiff; i++) {
          playCounts[i]=cursor.getInt(getColumnIndexForWeek(i - weekDiff));
        }
      }
      if (bumpCount) {
        playCounts[0]++;
      }
      float score=calculateScore(playCounts);
      if (score < .01f) {
        deleteEntry(database,stringId);
      }
 else {
        ContentValues values=new ContentValues(NUM_WEEKS + 2);
        values.put(SongPlayCountColumns.LAST_UPDATED_WEEK_INDEX,mNumberOfWeeksSinceEpoch);
        values.put(SongPlayCountColumns.PLAYCOUNTSCORE,score);
        for (int i=0; i < NUM_WEEKS; i++) {
          values.put(getColumnNameForWeek(i),playCounts[i]);
        }
        database.update(SongPlayCountColumns.NAME,values,WHERE_ID_EQUALS,new String[]{stringId});
      }
    }
 else     if (bumpCount) {
      ContentValues values=new ContentValues(2);
      int scoreIndex=cursor.getColumnIndex(SongPlayCountColumns.PLAYCOUNTSCORE);
      float score=cursor.getFloat(scoreIndex) + getScoreMultiplierForWeek(0);
      values.put(SongPlayCountColumns.PLAYCOUNTSCORE,score);
      values.put(getColumnNameForWeek(0),cursor.getInt(getColumnIndexForWeek(0)) + 1);
      database.update(SongPlayCountColumns.NAME,values,WHERE_ID_EQUALS,new String[]{stringId});
    }
    cursor.close();
  }
 else   if (bumpCount) {
    createNewPlayedEntry(database,id);
  }
  database.setTransactionSuccessful();
  database.endTransaction();
}

private void updateExistingRow ( final SQLiteDatabase database , final long id , boolean bumpCount ) { String stringId = String . valueOf ( id ) ; database . beginTransaction ( ) ; final Cursor cursor = database . query ( SongPlayCountColumns . NAME , null , WHERE_ID_EQUALS , new String [ ] { stringId } , null , null , null ) ; if ( cursor != null && cursor . moveToFirst ( ) ) { int lastUpdatedIndex = cursor . getColumnIndex ( SongPlayCountColumns . LAST_UPDATED_WEEK_INDEX ) ; int lastUpdatedWeek = cursor . getInt ( lastUpdatedIndex ) ; int weekDiff = mNumberOfWeeksSinceEpoch - lastUpdatedWeek ; if ( Math . abs ( weekDiff ) >= NUM_WEEKS ) { deleteEntry ( database , stringId ) ; if ( bumpCount ) { createNewPlayedEntry ( database , id ) ; } } else if ( weekDiff != 0 ) { int [ ] playCounts = new int [ NUM_WEEKS ] ; if ( weekDiff > 0 ) { for ( int i = 0 ; i < NUM_WEEKS - weekDiff ; i ++ ) { playCounts [ i + weekDiff ] = cursor . getInt ( getColumnIndexForWeek ( i ) ) ; } } else if ( weekDiff < 0 ) { for ( int i = 0 ; i < NUM_WEEKS + weekDiff ; i ++ ) { playCounts [ i ] = cursor . getInt ( getColumnIndexForWeek ( i - weekDiff ) ) ; } } if ( bumpCount ) { playCounts [ 0 ] ++ ; } float score = calculateScore ( playCounts ) ; if ( score < .01f ) { deleteEntry ( database , stringId ) ; } else { ContentValues values = new ContentValues ( NUM_WEEKS + 2 ) ; values . put ( SongPlayCountColumns . LAST_UPDATED_WEEK_INDEX , mNumberOfWeeksSinceEpoch ) ; values . put ( SongPlayCountColumns . PLAYCOUNTSCORE , score ) ; for ( int i = 0 ; i < NUM_WEEKS ; i ++ ) { values . put ( getColumnNameForWeek ( i ) , playCounts [ i ] ) ; } database . update ( SongPlayCountColumns . NAME , values , WHERE_ID_EQUALS , new String [ ] { stringId } ) ; } } else if ( bumpCount ) { ContentValues values = new ContentValues ( 2 ) ; int scoreIndex = cursor . getColumnIndex ( SongPlayCountColumns . PLAYCOUNTSCORE ) ; float score = cursor . getFloat ( scoreIndex ) + getScoreMultiplierForWeek ( 0 ) ; values . put ( SongPlayCountColumns . PLAYCOUNTSCORE , score ) ; values . put ( getColumnNameForWeek ( 0 ) , cursor . getInt ( getColumnIndexForWeek ( 0 ) ) + 1 ) ; database . update ( SongPlayCountColumns . NAME , values , WHERE_ID_EQUALS , new String [ ] { stringId } ) ; } cursor . close ( ) ; } else if ( bumpCount ) { createNewPlayedEntry ( database , id ) ; } database . setTransactionSuccessful ( ) ; database . endTransaction ( ) ; }
this function will take a song entry and update it to the latest week and increase the count for the current week by 1 if necessary
