-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);
}
}
打開兩個DB的&複製數據,我建議你使用FMDB包裝。它處理好sqlite !!! –
我認爲你沒有更新sqlite數據庫。更新數據庫之後,你看看sqlite是否真的更新了嗎? –
你已經發布了很多代碼。您需要將其縮小到相關部分,並明確指出哪些代碼會導致您遇到問題。請確保您也調用'sqlite3_errmsg'並記錄其結果,以便您知道問題的原因。 – rmaddy