我的应用程序用于每个月的费用,我有一个名为 Expense 的表包含 4 列(类型、ExpenseMonth、金额和名称),我现在每天都为我的费用插入数据我如何在一个部分中显示每天的费用表视图?
e.g :
如果日期是2-1-2013
我只想检索这一天的费用,依此类推。
我编写了以下查询,但每个部分都检索相同的数据。
const char *sql ="select Name from Expense where strftime('%m-%d',ExpenseMonth) Group by ExpenseMonth order by ExpenseMonth desc";
这是我更正后的代码....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *Dateo= [[NSMutableArray alloc]init];
NSMutableArray *NAMeA= [[NSMutableArray alloc]init ];
if (sqlite3_open([[AppDelegate getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql ="select ExpenseMonth,count(*) as day from Expense where strftime('%m',ExpenseMonth)=strftime('%m','now') group by ExpenseMonth order by ExpenseMonth desc";
sqlite3_stmt *selectstm;
int result = sqlite3_prepare_v2(database, sql, -1, &selectstm, NULL);
if( result== SQLITE_OK) {
while(sqlite3_step(selectstm) == SQLITE_ROW) {
NSString *dateAndTime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstm, 0)];
NSInteger numofday=sqlite3_column_int(selectstm,1);
MaxEntity *DatTime=[[MaxEntity alloc]initWithDate:dateAndTime andnumofDay:numofday];
[Dateo addObject:DatTime];
NSLog(@"Date %@",dateAndTime);
for (NSString *str in Dateo) {
NSString *querySQL =[NSString stringWithFormat:@"select Name, amount from Expense where ExpenseMonth=\"%@\" order by ExpenseMonth desc",dateAndTime];
const char *query_stmt = [querySQL UTF8String];
sqlite3_stmt *selectstmt;
int result = sqlite3_prepare_v2(database, query_stmt, -1, &selectstmt, NULL);
if( result== SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *NAMe=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSString *Amount=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
MaxEntity *NM=[[MaxEntity alloc]initWithName:NAMe andAmount:Amount];
[NAMeA addObject:NM];
MaxEntity *curItem = [NAMeA objectAtIndex:indexPath.section ]; //Get the model information at row location.
cell.textLabel.text = curItem.name;
NSLog(@"text%@",cell.textLabel.text);
[NAMeA removeLastObject];
[Dateo removeLastObject];
}
}
sqlite3_finalize(selectstmt);
}
}
}
sqlite3_finalize(selectstm);
sqlite3_close(database);
}
return cell;
}
它读取日期,然后根据日期选择数据,在完成检索数据后,它再次返回数据库以再次检索第一个日期并在此行中引发异常,请任何人帮助我停止选择语句什么时候检索所有日期???
MaxEntity *curItem = [NAMeA objectAtIndex:indexPath.section ];