如何在iphone中获取sqlite_step中的下一行

时间:2012-02-08 10:15:48

标签: iphone objective-c

我有一个应用程序,我试图通过对其余列进行计算来计算2列值。最初在查询字符串中我正在检索我正在通过选择查询来从表中检索所有行。然后我正在通过一个sqlite_step来检索我的表行并更新我的表。但是问题最初是第一行它进入sqlite_step并执行计算并使循环为true并返回sqlite_step以获取下一行但问题是何时它进入sqlite3_step == SQLITE_ROW它直接转到sqlite3_finalise而不进入循环并获取下一行。可能是什么问题。这是我的代码:

-(NSArray*)calculatesnoozefactor1:(NSString*)dbPath
{
    NSString *queryString = @"";
    int factor = 0;
    int factorIndex = 0;
    int x=0, y=0;
    int snoozecount = 0;
    int trigerredCount = 0;
    int tempSnoozeCount = 0;
    counter1 = 0;
    int snoozeFactorIndex = 0;
    NSMutableArray *array;
    int totalPercent = 0;
    long idalarm;
    //BOOL loop;
    long int firstTimeId;
    long int secondTimeId;
    NSString *snoozeOrStop;
    int total = 0;
    int triggered =0;
    int index = 1;
    NSString *timesetforAlarm;
    NSString *timealarm;
    int newid;
    int snoozeindex;
    NSMutableArray *suArray;
    NSMutableArray *percentarray;
    NSInteger rowInt= 0;

    NSMutableArray *newarray = [[NSMutableArray alloc]init];
    NSMutableArray *returnarray = [[NSMutableArray alloc]init];

    //@try 
    //  {
    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication] delegate];
    array = [[NSMutableArray alloc]init];
    [app copyDatabaseIfNeeded];
    sqlite3 *database;
    sqlite3_stmt *compiledStatement;
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {

        queryString = [NSString stringWithFormat:@"Select * from PenaltyTransaction"];
        int res = sqlite3_prepare_v2(database, [queryString UTF8String], -1, &compiledStatement, NULL);
        NSLog(@"%d",res);
        if (sqlite3_prepare_v2(database, [queryString UTF8String], -1, &compiledStatement, NULL) ==SQLITE_OK)
        {
            int res1 = sqlite3_step(compiledStatement);
            NSLog(@"%d",res1);
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSLog(@"Done");
                NSMutableArray *subArray = [[NSMutableArray alloc]init];
                if (loop == FALSE) {
                    //firstTimeId = sqlite3_column_int(compiledStatement, 0);
                    if (SQLITE_ROW)
                    {               
                firstTimeId = sqlite3_column_int(compiledStatement, 0);

                        NSLog(@"%d",SQLITE_ROW);


                        snoozeOrStop = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 6)];

                        if (![snoozeOrStop isEqualToString:@"stop"]) 
                        {

idalarm = sqlite3_column_int(compiledStatement, 0);
                            timesetforAlarm = [NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement, 1) ];
                            trigerredCount++;
                            snoozecount++;
                            tempSnoozeCount++;
                            counter1++;
                            factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                            NSString *queryString1 =[NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor= '%d' WHERE alarm_id='%d' AND snooze_time = '%@' AND alarm_date_time = '%@'",factor,idalarm,snoozeOrStop,timesetforAlarm];

                            int res = sqlite3_prepare_v2(database, [queryString1 UTF8String], -1, &compiledStatement, NULL);
                            NSLog(@"%d",res);
                            if (res == SQLITE_OK)
                            {
                                sqlite3_bind_int(compiledStatement,1 , factor);
                            }
                            if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                            {
                                NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                            }
                            else {
                                sqlite3_reset(compiledStatement);
                            }

                                      x  = factor;
                            snoozeFactorIndex = x-y;
                            NSLog(@"For the first Time the value of SnoozeFactor Index -------:%d",snoozeFactorIndex);
                            NSString *queryString2 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex='%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time ='%@'",snoozeFactorIndex,idalarm,snoozeOrStop,timesetforAlarm];
                            int res1 = sqlite3_prepare_v2(database, [queryString2 UTF8String], -1, &compiledStatement, NULL);
                            NSLog(@"%d",res1);
                            if (res1 == SQLITE_OK)
                            {
                                sqlite3_bind_int(compiledStatement, 1, snoozeFactorIndex);
                            }
                            if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                            {
                                NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                            }
                            else {
                                sqlite3_reset(compiledStatement);
                            }


                            y = x;
                            totalPercent = totalPercent + snoozeFactorIndex;
                            int index = snoozeFactorIndex;
                            [subArray addObject:[NSString stringWithFormat:@"%d",idalarm]];
                            [subArray addObject:timesetforAlarm];
                            [subArray addObject:[NSString stringWithFormat:@"%d",tempSnoozeCount]];
                            [subArray addObject:[NSString stringWithFormat:@"%d",index]];
                            [subArray addObject:snoozeOrStop];
                            [array addObject:subArray];

                        }
                        else {
                            triggered++;
                            trigerredCount++;
                            totalPercent = 0;
                            idalarm = sqlite3_column_int(compiledStatement, 0);
                            timesetforAlarm = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                            factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                            NSLog(@"@@@@@@@@:%d",factor);
                            NSString *queryString3 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor ='%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time = '%@'",factor,idalarm,snoozeOrStop,timesetforAlarm];
                            int res = sqlite3_prepare_v2(database,[queryString3 UTF8String] , -1, &compiledStatement, NULL);
                            NSLog(@"%d",res);
                            if (res ==SQLITE_OK)
                            {
                                sqlite3_bind_int(compiledStatement, 1, factor);

                            }
                            if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                            {
                                NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                            }
                            else {
                                sqlite3_reset(compiledStatement);
                            }

                            //sqlite3_finalize(compiledStatement);

                            x = factor;
                            NSLog(@"@@@@@@@Value of Snooze factor is @@@@@@@@@:%d",x);
                            snoozeFactorIndex = x-y;
                            NSLog(@"@@@@Value of SnoozeFactorIndex @@@@@@@:%d",snoozeFactorIndex);
                            NSString *queryString4 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozeFactorIndex = '%d' WHERE alarm_id ='%d' AND snooze_time = '%@' AND alarm_date_time = '%@'",snoozeFactorIndex,idalarm,snoozeOrStop,timesetforAlarm];
                            if (sqlite3_prepare_v2(database,[queryString4 UTF8String], -1,&compiledStatement, NULL) ==SQLITE_DONE)
                            {
                                sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                            }
                            if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                            {
                                NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                            }
                            else {
                                sqlite3_reset(compiledStatement);
                            }

                                                        y = x;
                            while (sqlite3_step(compiledStatement) == SQLITE_ROW)
                            {
                                int id1 = sqlite3_column_int(compiledStatement, 0);
                                int index = snoozeFactorIndex;
                                NSLog(@"@@@@@@@ id1 @@@@@@:%d",id1);
                                NSString *timeAlarm = [NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement,1) ];
                                [subArray addObject:[NSString stringWithFormat:@"%d",id1]];
                                [subArray addObject:timeAlarm];
                                [subArray addObject:[NSString stringWithFormat:@"%d",tempSnoozeCount]];
                                [subArray addObject:[NSString stringWithFormat:@"%d",index]];
                                [subArray addObject:snoozeOrStop];
                                [array addObject:subArray];

                            }

                        }
                    }loop = TRUE;



                }           



                else {
                    triggered++;
                    firstTimeId = sqlite3_column_int(compiledStatement, 0);
                    if (sqlite3_step(compiledStatement)== SQLITE_ROW)
                    {
                        secondTimeId = sqlite3_column_int(compiledStatement, 0);
                        NSLog(@"%d",secondTimeId);
                        snoozeOrStop = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,6) ];
                        timesetforAlarm = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];

                    }
                    if (secondTimeId != 0) {
                        if (firstTimeId == secondTimeId) {
                            if (![snoozeOrStop isEqualToString:@"stop"])
                            {
                                suArray = [[NSMutableArray alloc]init];
                                counter1 = 0;
                                for (int i = 0; i<[array count]; i++)
                                {
                                    suArray = [array objectAtIndex:i];
                                    BOOL isFalse = FALSE;
                                    if ([array count]!= 0)
                                    {
                                        NSString *idd =(NSString*)[suArray objectAtIndex:0];
                                        NSString *secondIdString = [NSString stringWithFormat:@"%d",secondTimeId];
                                        if ([idd isEqualToString:secondIdString])
                                        {
                                            NSString *st = (NSString*)[suArray objectAtIndex:4];
                                            if (![st isEqualToString:@"stop"])
                                            {
                                                counter1++;
                                                NSLog(@"#########:%d",counter1);
                                            }
                                        }
                                    }
                                }
                                trigerredCount++;
                                snoozecount++;
                                counter1++;
                                factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                                NSString *queryString5 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor ='%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time ='%@'",factor,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database, [queryString5 UTF8String],-1, &compiledStatement, NULL)==SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement,factor, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else 
                                {
                                    sqlite3_reset(compiledStatement);
                                }
                                                                x= factor;
                                snoozeFactorIndex = x-y;
                                NSLog(@"###### The value of factor is ### :%d and factor index is ######:%d",x,snoozeFactorIndex);
                                NSString *queryString6 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex ='%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time = '%@'",snoozeFactorIndex,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database, [queryString6 UTF8String],-1,&compiledStatement, NULL) == SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else 
                                {
                                    sqlite3_reset(compiledStatement);
                                }

                                while (sqlite3_step(compiledStatement)==SQLITE_ROW)
                                {
                                    y = x;
                                    total += snoozeFactorIndex;
                                    newid = sqlite3_column_int(compiledStatement, 0);
                                    timealarm = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                                    snoozeindex = snoozeFactorIndex;
                                    NSLog(@"####value of totalis###:%d and value of newId is##:%d and value of index is ##:%d",total,newid,snoozeindex);

                                }

                                [suArray addObject:[NSString stringWithFormat:@"%d",newid]];
                                [suArray addObject:timealarm];
                                [suArray addObject:[NSString stringWithFormat:@"%d",counter1]];
                                [suArray addObject:[NSString stringWithFormat:@"%d",snoozeindex]];
                                [suArray addObject:snoozeOrStop];
                                [array addObject:suArray];

                            }else {
                                counter1 = 0;
                                for (int i=0; i<[array count]; i++)
                                {
                                    suArray = [array objectAtIndex:i];
                                    if ([array count]!= 0)
                                    {
                                        NSString *id1 = (NSString*)[suArray objectAtIndex:0];
                                        NSString *secondString = [NSString stringWithFormat:@"%d",secondTimeId];
                                        NSString *idtime = [NSString stringWithFormat:@"%d",idalarm];
                                        if ([secondString isEqualToString:idtime])
                                        {
                                            NSString *st = (NSString*)[suArray objectAtIndex:4];
                                            if (![st isEqualToString:@"stop"])
                                            {
                                                counter1++;
                                            }
                                        }
                                    }
                                }
                            }

                            if (counter1 == 0)
                            {
                                trigerredCount++;
                                factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                                NSLog(@"**********Factor**********:%d",factor);

                                NSString *queryString7 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor ='%d' WHERE alarm_id = '%d'",factor,secondTimeId];
                                if (sqlite3_prepare_v2(database, [queryString7 UTF8String], -1,&compiledStatement, NULL) ==SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, factor, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else 
                                {
                                    sqlite3_reset(compiledStatement);
                                }
                                //sqlite3_finalize(compiledStatement);

                                x = factor;
                                snoozeFactorIndex = x-y;
                                NSLog(@"*****The value of SnoozeFactor***:%d and SnoozeFactor index is ***:%d",x,snoozeFactorIndex);
                                NSString *queryString8 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex ='%d' WHERE alarm_id = '%d'",snoozeFactorIndex,secondTimeId];
                                if (sqlite3_prepare_v2(database, [queryString8 UTF8String],-1,&compiledStatement, NULL)== SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                                }
                                if (sqlite3_step(compiledStatement)!=SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }

                                y =x;
                                while (sqlite3_step(compiledStatement)== SQLITE_ROW)
                                {
                                    NSString *timealarm = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                                    int index = snoozeFactorIndex;
                                    [suArray addObject:[NSString stringWithFormat:@"%d",secondTimeId]];
                                    [suArray addObject:timealarm];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",counter1]];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",index]];
                                    [suArray addObject:snoozeOrStop];
                                    [array addObject:suArray];
                                } 
                            }
                            else 
                            {
                                factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                                NSString *queryString9 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor ='%d' WHERE alarm_id = '%d' AND snooze_time = '%@' alarm_date_time ='%@'",factor,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database,[queryString9 UTF8String] ,-1,&compiledStatement, NULL) == SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, factor, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else 
                                {
                                    sqlite3_reset(compiledStatement);
                                }
                                //sqlite3_finalize(compiledStatement);

                                x= factor;
                                snoozeFactorIndex = x-y;
                                NSLog(@"%%%%%The value of Snooze factor is :%d and Snooze factor index is :%d",x,snoozeFactorIndex);
                                NSString *queryString10 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex ='%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time ='%@'",snoozeFactorIndex,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database, [queryString10 UTF8String], -1,&compiledStatement, NULL) == SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }


                                y = x;
                                while (sqlite3_step(compiledStatement)==SQLITE_ROW)
                                {
                                    NSString *timealarm = [NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement, 1)];
                                    int index = snoozeFactorIndex;
                                    [suArray addObject:[NSString stringWithFormat:@"%d",secondTimeId]];
                                    [suArray addObject:timealarm];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",counter1]];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",index]];
                                    [suArray addObject:snoozeOrStop];
                                    [array addObject:suArray];
                                }

                            }

                        }


                        else {
                            counter1 =0;
                            for (int i=0; i<[array count];i++ ) {
                                NSMutableArray *suArray = [array objectAtIndex:i];
                                if ([suArray count]!=0) {
                                    NSString *id1 = (NSString*)[suArray objectAtIndex:0];
                                    NSString *secondid = [NSString stringWithFormat:@"%d",secondTimeId];
                                    NSString *timeid = [NSString stringWithFormat:@"%d",firstTimeId];
                                    if ([secondid isEqualToString:timeid])
                                    {
                                        NSString *st = (NSString*)[suArray objectAtIndex:4];
                                        if (![st isEqualToString:@"stop"])
                                        {
                                            counter1++;
                                            NSLog(@"&&&&&&& Value of counter is:%d",counter1);
                                        }
                                    }
                                }
                            }
                            if (![snoozeOrStop isEqualToString:@"stop"])
                            {
                                counter1++;
                                trigerredCount++;
                                snoozecount++;
                                factor = (int)[self calculateSnoozeFactor:trigerredCount :snoozecount];
                                NSString *queryString11 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor ='%d' WHERE alarm_id = '%d' AND snooze_time = '%@' AND alarm_date_time ='%@'",factor,secondTimeId,snoozeOrStop,timesetforAlarm];
                                int res = sqlite3_prepare_v2(database,[queryString11 UTF8String] ,-1,&compiledStatement, NULL);
                                NSLog(@"%d",res);
                                if (res== SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement,factor,1 );
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {

                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                    sqlite3_close(database);
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }

                                x=factor;
                                snoozeFactorIndex = x-y;
                                NSLog(@"&&&&&& Value of SnoozeFactor:%d and SnoozeFactorIndex:%d",x,snoozeFactorIndex);

                                NSString *queryString12 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex ='%d'WHERE alarm_id ='%d' AND snooze_time='%@' AND alarm_date_time='%@'",snoozeFactorIndex,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database, [queryString12 UTF8String], -1,&compiledStatement, NULL) == SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }



                                y =x;
                                while (sqlite3_step(compiledStatement)==SQLITE_ROW)
                                {
                                    NSString *timealarm = [NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement,1) ];
                                    int index = snoozeFactorIndex;
                                    [suArray addObject:[NSString stringWithFormat:@"%d",secondTimeId]];
                                    [suArray addObject:timealarm];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",counter1]];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",index]];
                                    [suArray addObject:snoozeOrStop];
                                    [array addObject:suArray];
                                }
                            }                           else {
                                if (counter1 == 0) {
                                    trigerredCount++;
                                }
                                else {

                                }
                                factor = [self calculateSnoozeFactor:trigerredCount :snoozecount];
                                NSString *queryString13 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactor= '%d' WHERE alarm_id = '%d' AND snooze_time ='%@' AND alarm_date_time = '%@'",factor,secondTimeId,snoozeOrStop,timesetforAlarm];
                                int res = sqlite3_prepare_v2(database, [queryString13 UTF8String], -1, &compiledStatement, NULL);
                                NSLog(@"%d",res);
                                if (res ==SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, factor, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }

                                x = factor;
                                snoozeFactorIndex = x-y;
                                NSLog(@"$$$$$Value of snoozefactor:%d and snoozefactorindex is $$$$$:%d",x,snoozeFactorIndex);
                                NSString *queryString14 = [NSString stringWithFormat:@"UPDATE PenaltyTransaction SET snoozefactorindex = '%d' WHERE alarm_id ='%d' AND snooze_time ='%@' AND alarm_date_time = '%@'",snoozeFactorIndex,secondTimeId,snoozeOrStop,timesetforAlarm];
                                if (sqlite3_prepare_v2(database, [queryString14 UTF8String],-1,&compiledStatement, NULL)==SQLITE_OK)
                                {
                                    sqlite3_bind_int(compiledStatement, snoozeFactorIndex, 1);
                                }
                                if (sqlite3_step(compiledStatement)!= SQLITE_DONE)
                                {
                                    NSLog(@"Save Error:%s",sqlite3_errmsg(database));
                                }
                                else {
                                    sqlite3_reset(compiledStatement);
                                }

                                y = x;
                                while (sqlite3_step(compiledStatement)==SQLITE_ROW)
                                {
                                    NSString *timealarm = [NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement, 1) ];
                                    int index = 0;
                                    if (counter1 == 0)
                                    {
                                        index = snoozeFactorIndex;
                                    }else {
                                        index =0;
                                    }
                                    [suArray addObject:[NSString stringWithFormat:@"%d",secondTimeId]];
                                    [suArray addObject:timealarm];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",counter1]];
                                    [suArray addObject:[NSString stringWithFormat:@"%d",index]];
                                    [suArray addObject:snoozeOrStop];
                                    [array addObject:suArray];

                                }
                            }
                        }
                        firstTimeId = secondTimeId;
                        NSLog(@"The first id become+++++++:%d",firstTimeId);

                    }
                }
            }
            sqlite3_finalize(compiledStatement);


        }
        sqlite3_close(database);    
    }

    for (int i = [array count]-1; i>=0; i--)
    {
        NSMutableArray *temparray = [array objectAtIndex:i];
        [newarray addObject:temparray];
    }
    percentarray = [[NSMutableArray alloc] init];
    [percentarray addObject:newarray];
    int k = 0;
    for (int i = 0;i <[newarray count];i++)
    {
        NSMutableArray *a  = [newarray objectAtIndex:i];
        if ([a count]!=0)
        {
            NSString *id1 = (NSString*)[a objectAtIndex:0];
            for (int j=i +1;j<[newarray count] ; j++)
            {
                NSMutableArray *b = [newarray objectAtIndex:j];
                NSString *id2 = (NSString*)[b objectAtIndex:0];
                if ([id1 isEqualToString:id2])
                {
                    NSLog(@"id1========:%d  and id2============:%d",id1,id2);
                }
                [newarray removeObjectAtIndex:j];
                j--;
            }
        }
    }
    for (int i =0;i<[newarray count];i++)
    {
        NSMutableArray *anyArray = [newarray objectAtIndex:i];
        if ([anyArray count]!=0)
        {
            [returnarray addObject:anyArray];
        }
    }
    return returnarray;
}

//最初我从我的表PenaltyTransaction表中获取,如果它获取第一行我将循环设置为true,以便下次它不会检查第一行。这里的问题是当循环变为true,当它返回到while(sqlite3_step)== SQLITE_ROW时,它直接跳转循环并执行finalize语句。即使行中存在行,它也不会进入sqlite3_step。问题是什么。谢谢

1 个答案:

答案 0 :(得分:0)

这只是猜测,因为代码非常复杂......但是当循环为TRUE时,您似乎在设置secondTimeId时再次踩踏。所以你在这里是双重步,我认为你不想做。这就是你的while循环看起来如何删除一些代码:

while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                if (loop == FALSE) {
                    loop = TRUE;
                } else {
                    triggered++;
                    firstTimeId = sqlite3_column_int(compiledStatement, 0);
                    if (sqlite3_step(compiledStatement)== SQLITE_ROW)
                    {
                        secondTimeId = sqlite3_column_int(compiledStatement, 0);
                        NSLog(@"%d",secondTimeId);
                        snoozeOrStop = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,6) ];
                        timesetforAlarm = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                    }
                }
        }

正如您所看到的,如果此处没有行,if (sqlite3_step(compiledStatement)== SQLITE_ROW)之后将不会运行任何内容,因此它实际上将跳转到最终确定(但应设置firstTimeId)。