2013-03-13 96 views
0

我在iOS應用中進行同步。我在我的文檔文件夾中已經有一組表,並且正在將同步期間的服務器數據下載到一組新的表中。我正在比較各個表的數量,如果它們相同,我使用自然聯接來檢查它們實際上是否相同。如果它們是相同的,那麼我的內容是最新的,我不需要對當前的一組表進行任何更改,否則我將獲取新的一組表並將新數據存儲爲當前表。自然加入不能正常工作

問題是,當我對某些表進行自然連接時,雖然我在SQLITEMANAGER中看到兩個表的相同信息,但自然連接返回的答案不正確。不知道爲什麼。 count_together錯誤,即使值相同。

這是我實現我的檢查(僅僅是一個例子):

singleton.table = YES; 
    int count_table1 = 0; 
    int count_table2 = 0; 
    int count_together = 0; 

    NSMutableArray *arrayTables = [[NSMutableArray alloc] initWithObjects:@"DATA",@"TABLE",@"NUMBERS",nil]; 

    NSMutableArray *arrayTables2 = [[NSMutableArray alloc] initWithObjects:@"DATA2",@"TABLE2",@"NUMBERS2", nil]; 

    for(int i =0; i<[arrayTables count]; i++) 
    { 
     count_table1 = [databaseManager checkCountOfTable:[NSString stringWithFormat:@"SELECT COUNT(*) FROM %@",[arrayTables objectAtIndex:i]]]; 

     count_table2 = [databaseManager checkCountOfTable:[NSString stringWithFormat:@"SELECT COUNT(*) FROM %@",[arrayTables2 objectAtIndex:i]]]; 

     NSLog(@"Count of Table 1 is %i",count_table1); 
     NSLog(@"Count of Table 2 is %i",count_table2); 

     if (singleton.table == YES) 
     { 
      if(count_table1 != count_table2) 
      { 
       singleton.table = NO; 
      } 
      else 
      { 
       count_together = [databaseManager checkCountOfTable:[NSString stringWithFormat: @"SELECT COUNT(*) FROM (%@ NATURAL JOIN %@)",[arrayTables objectAtIndex:i],[arrayTables2 objectAtIndex:i]]]; 

       NSLog(@"Count of Table 1 is %i",count_table1); 
       NSLog(@"Count of Tables Together is %i",count_together); 

       if (count_table1 == count_together) 
       { 
        singleton.table = YES; 
       } 
       else 
       { 
        singleton.table = NO; 
       } 
      } 
     } 

    } 

需要一些指導,我歡迎建議。

回答

0

問題是表中有一些空列。因此這些表格上的自然連接不起作用。必須確保表沒有任何空列,並且NATURAL JOIN將正常工作。