2015-12-10 98 views
-1

我已經越來越接近解決我的代碼通過電子郵件打開導入sqlite數據庫在&打開,然後使用此數據來更新本地Sqlite3數據庫,我現在有它的工作,但只更新一行數據,我一直在試圖瞭解如何插入一個循環,但掙扎,任何人都可以引導我在正確的方向Xcode Sqlite3循環插入不工作

預先感謝

打開DB &保存到NSDefaults &插入到新的數據庫

-(void) updateLocalSqlite { 

    akpiRegionCPW1 = [[NSMutableArray alloc]init]; 

    const char *dbpath = [databasePath3 UTF8String]; 
    sqlite3_stmt *statement; 
    if (sqlite3_open(dbpath, &papav2Import) == SQLITE_OK) { 
     NSString *querySQL = [NSString stringWithFormat: 

           @"SELECT * FROM kpiRegionCPW"]; 


     const char *query_stmt = [querySQL UTF8String]; 
     if (sqlite3_prepare_v2(papav2Import, 
           query_stmt, -1, &statement, NULL) == SQLITE_OK) 


      while (sqlite3_step(statement) == SQLITE_ROW) { 
       NSString *ykpiRegionCPW1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; 
       NSString *ykpiRegionCPW2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
       NSString *ykpiRegionCPW3 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]; 
       NSString *ykpiRegionCPW4 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]; 
       NSString *ykpiRegionCPW5 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)]; 
       NSString *ykpiRegionCPW6 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]; 
       NSString *ykpiRegionCPW7 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)]; 
       NSString *ykpiRegionCPW8 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)]; 
       NSString *ykpiRegionCPW9 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)]; 
       NSString *ykpiRegionCPW10 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)]; 
       NSString *ykpiRegionCPW11 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 10)]; 
       NSString *ykpiRegionCPW12 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 11)]; 
       NSString *ykpiRegionCPW13 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 12)]; 
       NSString *ykpiRegionCPW14 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 13)]; 
       NSString *ykpiRegionCPW15 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 14)]; 
       NSString *ykpiRegionCPW16 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 15)]; 
       NSString *ykpiRegionCPW17 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 16)]; 
       NSString *ykpiRegionCPW18 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 17)]; 
       NSString *ykpiRegionCPW19 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 18)]; 
       NSString *ykpiRegionCPW20 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 19)]; 
       NSString *ykpiRegionCPW21 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 20)]; 
       NSString *ykpiRegionCPW22 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 21)]; 
       NSString *ykpiRegionCPW23 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 22)]; 
       NSString *ykpiRegionCPW24 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 23)]; 
       NSString *ykpiRegionCPW25 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 24)]; 
       NSString *ykpiRegionCPW26 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 25)]; 
       kpiRegionCPW1 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW1]; 
       kpiRegionCPW2 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW2]; 
       kpiRegionCPW3 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW3]; 
       kpiRegionCPW4 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW4]; 
       kpiRegionCPW5 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW5]; 
       kpiRegionCPW6 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW6]; 
       kpiRegionCPW7 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW7]; 
       kpiRegionCPW8 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW8]; 
       kpiRegionCPW9 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW9]; 
       kpiRegionCPW10 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW10]; 
       kpiRegionCPW11 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW11]; 
       kpiRegionCPW12 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW12]; 
       kpiRegionCPW13 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW13]; 
       kpiRegionCPW14 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW14]; 
       kpiRegionCPW15 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW15]; 
       kpiRegionCPW16 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW16]; 
       kpiRegionCPW17 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW17]; 
       kpiRegionCPW18 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW18]; 
       kpiRegionCPW19 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW19]; 
       kpiRegionCPW20 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW20]; 
       kpiRegionCPW21 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW21]; 
       kpiRegionCPW22 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW22]; 
       kpiRegionCPW23 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW23]; 
       kpiRegionCPW24 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW24]; 
       kpiRegionCPW25 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW25]; 
       kpiRegionCPW26 = [[NSString alloc]initWithFormat:@"%@", ykpiRegionCPW26]; 

       [akpiRegionCPW1 addObject:kpiRegionCPW1]; 
      } 

     sqlite3_finalize(statement); 

     char *err; 
     if (sqlite3_exec(papav2Import, [querySQL UTF8String], NULL, NULL, &err) !=SQLITE_OK) { 
      sqlite3_close(papav2Import); 

      NSLog(@"Export Failed!!!!"); 

     }else{ 
      NSLog(@"Export Worked"); 

     } 


      } 
    sqlite3_close(papav2Import); 
    NSLog(@"Test Export: %@", akpiRegionCPW1); 

    { 

      [self openDBLocal]; 

      const char *sqlStatement = "INSERT INTO kpiRegionCPW (CPW_Region,Region_Number,Region_Name,Regional_General_Manager,Period,EBIT_YTD,EBIT_12Month,Sales_Conversion,Sales_Conversion_VsTarget,PPCR,PPCR_VsTarget,GSHT,Upgrade_Mix,Upgrade_Mix_VsTarget,WCSS,WCSS_VsTarget,Right_Sell_Usage,Right_Sell_Usage_VsTarget,Mystery_Shopper,Mystery_Shopper_VsTarget,SOH_Spend,SOH_Spend_VsTarget,CATI,CATI_VsTarget,Shrinkage,Shrinkage_VsTarget)VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

      sqlite3_stmt *stmt; 

      if(sqlite3_prepare_v2(localDB, sqlStatement, -1, &stmt, NULL) == SQLITE_OK) 

      { 
       for (int i = 0; i < akpiRegionCPW1.count; i++) { 


        sqlite3_bind_text(stmt, 1, [kpiRegionCPW1 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 2, [kpiRegionCPW2 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 3, [kpiRegionCPW3 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 4, [kpiRegionCPW4 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 5, [kpiRegionCPW5 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 6, [kpiRegionCPW6 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 7, [kpiRegionCPW7 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 8, [kpiRegionCPW8 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 9, [kpiRegionCPW9 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 10, [kpiRegionCPW10 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 11, [kpiRegionCPW11 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 12, [kpiRegionCPW12 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 13, [kpiRegionCPW13 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 14, [kpiRegionCPW14 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 15, [kpiRegionCPW15 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 16, [kpiRegionCPW16 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 17, [kpiRegionCPW17 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 18, [kpiRegionCPW18 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 19, [kpiRegionCPW19 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 20, [kpiRegionCPW20 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 21, [kpiRegionCPW21 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 22, [kpiRegionCPW22 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 23, [kpiRegionCPW23 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 24, [kpiRegionCPW24 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 25, [kpiRegionCPW25 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(stmt, 26, [kpiRegionCPW26 UTF8String], -1, SQLITE_TRANSIENT); 

        if (sqlite3_step(stmt) == SQLITE_DONE) { 
         if (i == (akpiRegionCPW1.count - 1)) 

          sqlite3_finalize(stmt); 
         else 
          sqlite3_reset(stmt); 
        } 
        else { 
         NSLog(@"row insertion error"); 
        } 
       } 
      } 

     sqlite3_close(localDB); 

     } 

} 
+0

打開兩個DB的&複製數據,我建議你使用FMDB包裝。它處理好sqlite !!! –

+0

我認爲你沒有更新sqlite數據庫。更新數據庫之後,你看看sqlite是否真的更新了嗎? –

+0

你已經發布了很多代碼。您需要將其縮小到相關部分,並明確指出哪些代碼會導致您遇到問題。請確保您也調用'sqlite3_errmsg'並記錄其結果,以便您知道問題的原因。 – rmaddy

回答

0

很多衝浪&閱讀後,我最終得到了那裏,徑直穿過

- (void)updateLocalSqlite { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    NSString *maindbPath = [documentsDir stringByAppendingPathComponent:@"newDataPapav2.sqlite"];; 

    NSString *newdbPath = [documentsDir stringByAppendingPathComponent:@"KpiPapav2.sqlite"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    char *error; 

    if ([fileManager fileExistsAtPath:newdbPath]) { 
    } 
    sqlite3 *database; 

    if (sqlite3_open([newdbPath UTF8String], &database)!=SQLITE_OK) { 
     NSLog(@"Error to open database"); 
    } 

    NSString *attachQuery = [NSString stringWithFormat:@"ATTACH DATABASE \"%@\" AS aDB",maindbPath]; 

    sqlite3_exec(database, [attachQuery UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to Attach = %s",error); 
    } 

    NSString *sqlString = @"INSERT INTO kpiRegionCPW SELECT * FROM aDB.kpiRegionCPW"; 
    sqlite3_exec(database, [sqlString UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to copy kpiRegionCPW table = %s",error); 
    } 

    NSString *sqlString2 = @"INSERT INTO kpiRegion SELECT * FROM aDB.kpiRegion"; 
    sqlite3_exec(database, [sqlString2 UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to copy kpiRegion table = %s",error); 
    } 

    NSString *sqlString3 = @"INSERT INTO kpiStoreCPW SELECT * FROM aDB.kpiStoreCPW"; 
    sqlite3_exec(database, [sqlString3 UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to copy kpiStoreCPW table = %s",error); 
    } 

    NSString *sqlString4 = @"INSERT INTO kpiStore SELECT * FROM aDB.kpiStore"; 
    sqlite3_exec(database, [sqlString4 UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to copy kpiStore table = %s",error); 
    } 

    NSString *sqlString5 = @"REPLACE INTO dataPPR SELECT * FROM aDB.dataPPR"; 
    sqlite3_exec(database, [sqlString5 UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to REPLACE dataPPR table = %s",error); 
    } 

    NSString *sqlString6 = @"REPLACE INTO storeDetails SELECT * FROM aDB.storeDetails"; 
    sqlite3_exec(database, [sqlString6 UTF8String], NULL, NULL, &error); 
    if (error) { 
     NSLog(@"Error to REPLACE storeDetails table = %s",error); 
    } 

    NSLog(@"database update success"); 
    sqlite3_close(database); 
}