After hours of fighting on SO and using the above post, finally I was able to create Objective-C code for this.
NSString* dbPath1; NSString* dbPath2; dbPath1 = [self getDB1Path]; //This db have the desired table to be copied dbPath2 = [self getDB2Path]; //This needs to have the desired table //open database which contains the desired "table" if (sqlite3_open(dbPath1.UTF8String, &databasePhase2) == SQLITE_OK) { NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \"%@\" AS phase2_db",dbPath2]; const char *attachSQLChar = [attachSQL UTF8String]; char* errInfo; int result = sqlite3_exec(databasePhase2, attachSQLChar, nil, nil, &errInfo); if (SQLITE_OK == result) { NSLog(@"new db attached"); NSString *attachSQL = [NSString stringWithFormat: @"CREATE TABLE newTableInDB1 AS SELECT * FROM phase2_db.qmAyahInfo"]; const char *createSQLChar = [attachSQL UTF8String]; int result2 = sqlite3_exec(databasePhase2, createSQLChar, nil, nil, &errInfo); if (SQLITE_OK == result2) { NSLog(@"New table created in attached db"); } } sqlite3_close(databasePhase2); }
source share