从表视图中的每个部分检索sqlite的数据

时间:2013-01-20 12:05:17

标签: ios sqlite

我的应用程序是每个月的费用,我有名称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;

}

它读取日期,然后根据日期选择数据,完成检索数据后再次返回数据库再次检索第一个日期并在此行中抛出异常,Plz可以任何人帮我停止检索所有日期时的select语句???  MaxEntity *curItem = [NAMeA objectAtIndex:indexPath.section ];

1 个答案:

答案 0 :(得分: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语句(替换“%@”)。