]我有这个图表显示我的sqlite数据库中的最后7条记录,它可以工作。
但我想知道绝对的最后7天。
这是我的代码
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.
}
答案 0 :(得分:3)
如果不经过这些代码块,我会在这里猜测:
SELECT * FROM CONSUMED WHERE DATE BETWEEN ? AND ?
设置7天前的日期(7 * 24 * 60 * 60 * 1000毫秒前),以及现在,并将它们绑定到问号。