2012-02-08 9 views
0

私は2つの列の値を計算することを試みているアプリケーションを持っています。最初にクエリー文字列に計算を実行して、私はretreiveです私はsqlite_stepを渡しています。私はsqlite_stepを渡して私のテーブルの行を取得し、テーブルを更新しています。最初の行の問題は最初はsqlite_stepに入り、計算を実行してループをtrueにしてsqlite_step == SQLITE_ROWそれが直接ループに入ると、次row.Whatがproblem.Thisかもしれフェッチせずにsqlite3_finaliseに行く次の行をフェッチしますが、それはsqlite3_stepに入ったときに問題があるためには、私のコードです:iphoneでsqlite_stepの次の行を取得する方法

-(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テーブルからフェッチしています。もし最初の行を取得したら、私は自分のループを真にしていますので、最初の行をチェックしません。 while(sqlite3_step)== SQLITE_ROWに戻ると、ループを直接ジャンプしてfinalize文を実行します。行が存在していてもsqlite3_stepには入りません。問題点は何ですか?

+2

これは非常に長い記述であり、私は誰もそれを読んでそれを理解してからこれに答えるのに多くの時間があるとは思っていません。 –

答えて

0

これはコードがかなり複雑なので推測です...ループがTRUEの場合、secondTimeIdを設定すると再びステップしているようです。だから私はあなたがやりたいとは思っていないここでダブルステッピングしています。あなたが見ることができるように行がここに存在しない場合は、何もif (sqlite3_step(compiledStatement)== SQLITE_ROW)後に実行されませんので、それが実際にジャンプが確定します(ただしfirstTimeIdがすべき

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)]; 
        } 
       } 
     } 

:これは、いくつかのコード削除を使用している間はループをどのように見えます設定する)。

+0

私はfirstTimeIdを設定する必要があります。ループのステートメントまたはsqlite_stepの前に。 – Rani

+0

いいえ、 'while(sqlite3_step(compiledStatement)== SQLITE_ROW)'の後に 'if sqlite3_step(compiledStatement); '。あまりにも多くのことが起こっているので、私はあなたが何をする必要があるかを詳細に知ることができません。 – vakio

+0

@vaiko私の問題は、9列のPenaltyTransactionという名前のデータベーステーブルを持つことです。カラム名はalarmid、alarmtime、penaltytype、penaltyamt、currentbal、newbal、snoozetime、snoozefactor、snoozefactorindexです。最後の2カラムでは、このメソッドが呼び出され、計算が実行されるまでデータは挿入されません。 PenaltytableとqueryStringで保存し、私はテーブル内のすべての行をループし、sqlite_stepループで私は第1行を取得するための別のチェックを実行しているようにsqlite3_stepを使用しています。 – Rani

関連する問題