Xcode Sqlite3循环插入不起作用

时间:2015-12-10 15:56:31

标签: objective-c sqlite

我已经越来越接近解决我的代码,通过电子邮件打开导入sqlite数据库&然后使用这些数据来更新本地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);

        }

}

1 个答案:

答案 0 :(得分:0)

经过大量的冲浪和阅读我最后到达那里,打开了DB和&amp;直接复制数据

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