0

我的应用程序用于每个月的费用,我有一个名为 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 ];

4

1 回答 1

0

select Name from Expense where strftime('%m-%d',ExpenseMonth) Group by ExpenseMonth order by ExpenseMonth desc

你的 where 子句只计算一个字符串值,它不比较任何东西,所以它总是返回相同的结果。它需要采用where ExpenseMonth = .... 因此,对于给定的部分/日期,您需要设置如下查询:

NSString *sql = [NSString stringWithFormat:@"select Name from Expense where ExpenseMonth = %@ Group by ExpenseMonth order by ExpenseMonth desc", sectionDate];

(抱歉,这是在 Objective-C 中,这是我的工作,但应该让你知道如何在 C++ 中做到这一点,我认为这是你正在工作的。)

基本上,循环遍历将日期字符串 (sectionDate) 替换为表的每个部分的 SQL 语句(替换“%@”)的日期。

于 2013-01-20T15:37:10.157 回答