2011-08-30 64 views
0

]我有這張圖顯示了我的sqlite數據庫中的最後7條記錄,它的工作原理。如何獲得絕對最後7天的數據,而不是SQLite iPhone中的最後7條記錄

但我想知道過去7天的絕對值。

enter image description here

這是我的代碼

if ([appDelegate.drinksOnDayArray count] >=7) 
    { 
     drunked = 7; 
    } 
    else { 
     drunked = [appDelegate.drinksOnDayArray count]; 
    } 


    if (drunked !=0) 
    { 
     if (drunked<7) { 
     for (int i=drunked; i<7; i++) { 

      //DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i-1]; 

      NSString * dayString= [NSString stringWithFormat:@"Nil"];//[NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed]; 

      [dayArray addObject:dayString];//X label for graph the day of drink. 

      } 
     } 

     for(int i=drunked; i>0; i--) 
     { 
      DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i-1]; 

      NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed]; 

      [dayArray addObject:dayString];//X label for graph the day of drink. 

      drinksOnDay.isDetailViewHydrated = NO; 

      [drinksOnDay hydrateDetailViewData]; 

      NSNumber *sdNumber = drinksOnDay.standardDrinks; // pass value over to Standard Drink Numbers 

      //[sdArray addObject: sdNumber]; 

      float floatNum = [sdNumber floatValue]; // convert sdNumber to foat 

      [sdArray addObject:[NSNumber numberWithFloat:floatNum]];//add float Value to sdArray 
    } 




    //Get max value using KVC 


    fMax = [[sdArray valueForKeyPath:@"@max.floatValue"] floatValue]; 


    //Ceiling the max value 
    imax = (int)ceilf(fMax); 

    //Odd check to make even by checking right most bit 
    imax = (imax & 0x1) ? imax + 1 : imax; 

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:(imax/2) + 1]; 

    //Assuming all numbers are positive 

    for(int i = 0; i <= imax; i +=2) 
    { 
     [array addObject:[NSString stringWithFormat:@"%d", i]]; 
    } 

    NSLog(@"Array Value %@", array); 

    NSLog(@"Day Array%@", dayArray); 



    NSString *sData[drunked];// = malloc(7 * sizeof(NSString *)); 
    for (int i=0; i<drunked; i++) 
    { 
     DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i]; 
     sData[i] = [NSString stringWithFormat:@"%@",drinksOnDay.standardDrinks]; 

    } 
    NSLog(@"sdArray %@",sdArray); 
    if (drunked<7) { 
     for (int i=drunked; i<7; i++) { 
      sData[i]=[NSString stringWithFormat:@"0"]; 
     } 
    } 

我的SQLite聲明

- (void) hydrateDetailViewData { 

    //If the detail view is hydrated then do not get it from the database. 
    if(isDetailViewHydrated) return; 

    self.standardDrinks = [NSDecimalNumber zero]; 
    NSDecimalNumber *decimal = [NSDecimalNumber zero]; 

    if(detailStmt == nil) { 
     const char *sql = "SELECT volume, percentage FROM consumed WHERE DATE(datetime) = ?"; 
     if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) != SQLITE_OK) 
      NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database)); 
    } 

    sqlite3_bind_text(detailStmt, 1, [[NSDate stringFromDate:self.dateConsumed withFormat:@"yyyy-MM-dd"] UTF8String], -1, SQLITE_TRANSIENT); 

    static NSDecimalNumberHandler* roundingBehavior = nil; 

    if (roundingBehavior == nil) { 
     roundingBehavior = 
     [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO]; 
    }  

    while(sqlite3_step(detailStmt) == SQLITE_ROW) { 
     double volumeDN = sqlite3_column_double(detailStmt, 0); 
     double percentageDN = sqlite3_column_double(detailStmt, 1); 
     double drinks = ((volumeDN/1000) * percentageDN); 
     NSDecimalNumber *drinksDec = [[NSDecimalNumber alloc] initWithDouble:drinks]; 
     NSDecimalNumber *countryRate = [[NSDecimalNumber alloc] initWithDouble:0.789]; 
     decimal = [decimal decimalNumberByAdding:[drinksDec decimalNumberByMultiplyingBy:countryRate]]; 
     //NSLog([NSString stringWithFormat:@"Standard Drinks - %@", self.standardDrinks]); 
    } 

    self.standardDrinks = [decimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior]; 


    //Reset the detail statement so it can be used again without preparing the statement again. 
    sqlite3_reset(detailStmt); 

    //Set isDetailViewHydrated as YES, so we do not get it again from the database. 
    isDetailViewHydrated = YES; 
} 


+ (void) getInitialDataToDisplay:(NSString *)dbPath { 

    DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 

     const char *sql = "SELECT DATE(datetime) FROM consumed GROUP BY DATE(datetime) ORDER BY datetime DESC"; 
     sqlite3_stmt *selectstmt; 
     if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { 

      while(sqlite3_step(selectstmt) == SQLITE_ROW) { 
       NSString *dateDrunk = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)]; 
       NSDate *theDate = [NSDate dateFromString:dateDrunk withFormat:@"yyyy-MM-dd"]; 
       DayOfDrinks *drinkDayObj = [[DayOfDrinks alloc] initWithDateConsumed:theDate]; 
       [drinkDayObj hydrateDetailViewData]; 
       //NSLog([NSDate stringFromDate:drinkDayObj.dateConsumed withFormat:@"yyyy-MM-dd"]); 
       [appDelegate.drinksOnDayArray addObject:drinkDayObj]; 
       [drinkDayObj release]; 
      } 
     } 
    } 
    else 
     sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory. 

}

回答

3

沒有通過這些代碼塊一起去,我會在這裏大膽地猜測:

SELECT * FROM CONSUMED WHERE DATE BETWEEN ? AND ? 

設置7天前的日期(7 * 24 * 60 * 60 * 1000毫秒前),以及現在,並將其綁定到問號。

+0

嗨bdares, 你可以給我一個例子如何做到這一點? – Desmond

相關問題