2014-06-25 61 views
0

因此,我的目標是:我需要將同步數據庫附加到我的主數據庫,並更新或替換我的主數據庫中的任何字段。所以我先把附上我的數據庫。然後我試圖通過所有的表格。這裏是一個古怪的部分:在我的主查詢字符串內部,當我說:SELECT name FROM sqlite_master if語句不執行並且說「錯誤:不是錯誤」。現在,當我將主查詢告訴SELECT名稱FROM sync_db.sqlite_master時,執行if語句。但是,我收到一條錯誤消息,說沒有這樣的表:sync_db.sqlite_master存在。有人可能會通過適當的協議走過我嗎?提前致謝。附加數據庫時錯誤不是錯誤的sqlite數據庫

//Atataching the sync db to the master db 

    NSString *attachSQL = [NSString stringWithFormat:@"ATTACH DATABASE \'%@\' AS sync_db", dbPathSync]; 

    NSLog(@"Here's the arratch string: %@", attachSQL); 

    // 
    if ((errorNum = sqlite3_exec(mainOpenHandle, [attachSQL UTF8String], NULL, NULL, &errorMessage)) == SQLITE_OK) { 

     NSString *masterQuery = [NSString stringWithFormat:@"SELECT name FROM sync_db.sqlite_master WHERE type='table';"]; 
     const char *masterStmt = [masterQuery UTF8String]; 
     sqlite3_stmt *statement; 

     //If statement does not execute and prints error saying "not an error" when 
     //place SELECT from "sqlite_master" inside master query. 
     if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)) { 
      while (sqlite3_step(statement) == SQLITE_ROW) { 

       NSString * currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 

       NSLog(@"Here's the current table: %@",currentTable); 

       //This is where the magic happens. If there are any keys matching the database, it will update them. If there are no current keys in the database, the query will insert them. 
       if ([currentTable isEqualToString:@"USER_DATA"] == NO && [currentTable isEqualToString:@"USER_ACTIVITY"]== NO && [currentTable isEqualToString:@"USER_ITINERARY"] == NO) { 
        NSString *tblUpdate = [NSString stringWithFormat:@"INSERT or REPLACE INTO main.%@ SELECT * FROM sync_db.%@;",currentTable, currentTable]; 
        const char *updateStmt = [tblUpdate UTF8String]; 
        if ((errorNum = sqlite3_exec(mainOpenHandle, updateStmt, NULL, NULL, &errorMessage))!= SQLITE_OK) { 

         if (errorNum == 1) { 
          //A database reset is needded 

          self->isResetDataBase = YES; 
         } 
         dbErr = YES; 
        } 
       } 
      } 
      NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle)); 
     } 
     NSLog(@"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(mainOpenHandle)); 

     NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle)); 
     sqlite3_finalize(statement); 
     //Detaching the database from the mainDB 
     NSString *detachSQL = [NSString stringWithFormat:@"DETACH DATABASE sync_db"]; // reference sync db 
     if ((errorNum = sqlite3_exec(mainOpenHandle, [detachSQL UTF8String], NULL, NULL, &errorMessage))!= SQLITE_OK) { 

      NSLog(@"Detatched syncDb Failed. ErrorMessage = %s ", errorMessage); 


     } 
    } 


} 



NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle)); 

//Closing the database when finished. 
if (mainOpenHandle != nil) { 
    sqlite3_close(self.mainOpenHandle); 
} 

if (syncOpenHandle != nil) { 
    sqlite3_close(self.syncOpenHandle); 
    NSError *err; 
    int success = [fileManager fileExistsAtPath:dbPathSync]; 
    if (success) { 
     [[NSFileManager defaultManager]removeItemAtPath:dbPathSync error: &error]; 
    } 

} 

if (userOpenHandle != nil) { 
    sqlite3_close(self.userOpenHandle); 
} 

然後我嘗試遍歷所有行。但這是一個古怪的部分。

回答

1

您應該將sqlite3_prepare_v2的結果與SQLITE_OK進行比較。

如果只是做:

if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)) { 

那麼if語句只會如果有錯誤成功。你想:

if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL) == SQLITE_OK) { 

你也應該更新代碼,只在if聲明else塊記錄錯誤。

if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL) == SQLITE_OK) { 
    // process query 
} else { 
    // log error here 
}