我需要一些幫助才能從SQLite獲取最近7天的日期值。即使數據庫中沒有記錄,也能從sqllite獲取最後7天的日期值
我目前能夠從SQLite獲得日期值,如果有記錄。
笏我需要的是顯示過去7天,如果這傢伙做了什麼。
例如,如果人只能喝啤酒的今天,它會顯示
drink - thur
no - wed
no - tue
no - mon
no - sun
no - sat
no - sat
例如,如果人沒喝任何東西,它會顯示
no - thur
no - wed
no - tue
no - mon
no - sun
no - sat
no - sat
例如,如果人喝任何東西在過去7天內,它會顯示
no - thur
drink - wed
no - tue
no - mon
drink - sun
no - sat
no - sat
這是SQL代碼
+ (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.
}
DrinkHistoryTableViewController.m
if (drunked<7) {
for (int i=drunked; i<7; i++) {
NSString * dayString= [NSString stringWithFormat:@"Nil"];/
[dayArray addObject:dayString];
}
}
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
}
任何人可以幫助回答我的問題非常感謝。
Des
你能提供一些額外的細節嗎?例如:數據庫的結構,你如何存儲日期,你如何查詢最近七次,什麼「不」意味着什麼(僅僅是一個'nil'還是一個特殊值?)等等。 –
這意味着NO是來自sqldata的空值,它將添加到零值中,但仍顯示日期值和零值 – Desmond
感謝您的回覆:) – Desmond