【发布时间】:2018-10-29 20:40:42
【问题描述】:
我正在尝试编写一种方法,该方法将返回我的“成本”表中所有成本的总和,但在本周(以星期一为第一天)
这就是我的数据库的样子:
这是我里面的数据
我有这样的事情:
- (float)getthisweekscosts {
float sumprice = 0; // We initialize the total price for all the rows
NSString *getcurrentcost = [NSString stringWithFormat:@"%@", @"SELECT COSTAMOUNT FROM COST WHERE DATE > DATE('now', 'weekday 0', '-7 days')"];
const char *query_stmt_budget = [getcurrentcost UTF8String];
if (sqlite3_prepare_v2(costDB,query_stmt_budget, -1, &statement, NULL) == SQLITE_OK){
while (sqlite3_step(statement) == SQLITE_ROW){
NSString *currentcost = [NSString stringWithFormat:@"%s",(const char *) sqlite3_column_text(statement, 0)];
sumprice += [currentcost floatValue];
}
sqlite3_finalize(statement);
}
sqlite3_close(costDB);
return sumprice;
}
来源:Get this week's data using SQLite
但我不确定为什么它在我的情况下返回 0。
【问题讨论】:
标签: sql objective-c sqlite date datetime