2011-06-02 7 views
-1

私はデータベースを持っています。それには2つのテーブルがあります。 ifの条件で1つのテーブルに電話したいと思います。 ifの条件が満たされない場合、else部分の2番目のテーブルを呼び出すにはどうすればよいですか?iPhoneの同じデータベースの "ifとelse"部分で異なるテーブルを呼び出す方法は?

これは私が使用するコードです:

{ 
    NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *DocumentsDirectory = [Paths objectAtIndex:0]; 
    NSString *Path = [DocumentsDirectory stringByAppendingPathComponent:@"StoreList.sqlite"]; 

    // Open the database from the users filessytem. 
    if (sqlite3_open([Path UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     //*********const char *sqlStatement = "select * from Store where Zipcode ='%@'",inputTxt ; 

     NSString *sqlStatement = [NSString stringWithFormat:@"select * from Store where Zipcode ='%@' or Address = '%@' or CityName = '%@'",inputTxt, inputTxt, inputTxt]; 
     NSLog(@" Query in if :%@",sqlStatement); 
     sqlite3_stmt *compiledStatement; 

     if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) { 

      // Loop through the results and add them to the feeds array. 

      if(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 

       NSString *latValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSLog(@"Latitude:%@",latValue); 
       NSString *longValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
       NSLog(@"Longitude:%@",longValue); 
       //currentLocationLbl.text=[NSString stringWithFormat:@" %@ , %@" ,latValue,longValue]; 

       // delegate.latitudeVal=latValue; 
       // delegate.longitudeVal=longValue; 
       txtChangeLocation.text = @""; 
       isFromChangeLoc=TRUE; 

       //self.tabBarController.selectedIndex=3; 
      } 
      else { 
       NSLog(@"ELSE PART"); 

       // Open the database from the user's filessytem. 
       if (sqlite3_open([Path UTF8String], &database) == SQLITE_OK) { 
        // Setup the SQL Statement and compile it for faster access 
        //*********const char *sqlStatement = "select * from Store where Zipcode ='%@'",inputTxt ; 

        NSString *sqlStatement = [NSString stringWithFormat:@"select * from zipcodes where zip ='35004'"]; 
        NSLog(@" Query in if :%@",sqlStatement); 
        sqlite3_stmt *compiledStatement; 

        if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) { 
         // Loop through the results and add them to the feeds array 

         if(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
          // Read the data from the result row 

          NSString *latValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
          NSLog(@"Latitude:%@",latValue); 
          NSString *longValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
          NSLog(@"Longitude:%@",longValue); 
          //currentLocationLbl.text=[NSString stringWithFormat:@" %@ , %@" ,latValue,longValue]; 

          // delegate.latitudeVal=latValue; 
          // delegate.longitudeVal=longValue; 
          txtChangeLocation.text = @""; 
          isFromChangeLoc=TRUE; 
         } 
        } 
       } 
      } 
      sqlite3_finalize(compiledStatement); 
      sqlite3_close(database); 
     } 
    } 

私はテキストボックスから入力を取得しています。データベースにある正しい値を与えると、正常に動作しています。データのフェッチは正しいです。テキストボックスに間違ったデータを入力した場合、正常に動作していない - else状態が失敗します。

この問題を解決するにはどうすればよいですか?

答えて

0

elseに行くと、同じデータベースをもう一度開く必要はありません(いくつかの問題が発生する可能性がありますが、試していない可能性があります)。最初の(選択)ステートメントを実行するときはBOOLを使用し、失敗した場合はYESまたはNOに設定します。最初のステートメントを終了した後、BOOLの値を確認し、==NOが2番目のステートメントを実行しているかどうかを確認します。

... 
BOOL success = NO; 
NSString *sqlStatement = [NSString stringWithFormat:@"select * from Store where Zipcode '%@' or Address = '%@' or CityName = '%@'",inputTxt, inputTxt, inputTxt]; 
NSLog(@" Query in if :%@",sqlStatement); 
sqlite3_stmt *compiledStatement; 
if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK){ 
    if(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
     // Read the data from the result row 

     NSString *latValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
     NSLog(@"Latitude:%@",latValue); 
     NSString *longValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
     NSLog(@"Longitude:%@",longValue); 
     //currentLocationLbl.text=[NSString stringWithFormat:@" %@ , %@" ,latValue,longValue]; 

    // delegate.latitudeVal=latValue; 
    // delegate.longitudeVal=longValue; 
     txtChangeLocation.text = @""; 
     isFromChangeLoc=TRUE; 

     //self.tabBarController.selectedIndex=3; 
     success=YES; 
    } 
} 
sqlite3_finalize(compiledStatement); 

if(success){ 
    //do the else from your code 
    NSString *sqlStatement = [NSString stringWithFormat:@"select * from zipcodes where zip ='35004'"]; 
    NSLog(@" Query in if :%@",sqlStatement); 

    if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK){ 
     // Loop through the results and add them to the feeds array 

     if(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
     // Read the data from the result row 


       NSString *latValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSLog(@"Latitude:%@",latValue); 
       NSString *longValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
       NSLog(@"Longitude:%@",longValue); 
       //currentLocationLbl.text=[NSString stringWithFormat:@" %@ , %@" ,latValue,longValue]; 

      // delegate.latitudeVal=latValue; 
      // delegate.longitudeVal=longValue; 
       txtChangeLocation.text = @""; 
       isFromChangeLoc=TRUE; 
     } 
    } 
    sqlite3_finalize(compiledStatement); 
} 

私はそれを実行していないので、いくつかのバグが含まれている可能性があります。

関連する問題