Copy table in one bit to another db

I want to copy a table from aDB to another bDB.

So, I made a method. I think that opening database 2 and using the insert query will work, but I don't know in detail.

-(void)copyDatabaseTableSoruceFileName:(NSString *)source CopyFileName:(NSString *)copy { sqlite3 *sourceDatabase=NULL; sqlite3 *copyDatabase=NULL; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDir = [paths objectAtIndex:0]; //source [self copyFileIfNeed:source path:documentDir]; NSString *SourceDBPath = [documentDir stringByAppendingPathComponent:source]; if( sqlite3_open([SourceDBPath UTF8String],&sourceDatabase)!= SQLITE_OK ) { NSLog(@"DB File Open Error :%@", SourceDBPath); sourceDatabase = NULL; } //copy [self copyFileIfNeed:copy path:documentDir]; NSString *CopyDBPath = [documentDir stringByAppendingPathComponent:copy]; if( sqlite3_open([CopyDBPath UTF8String],&copyDatabase)!= SQLITE_OK ) { NSLog(@"DB File Open Error :%@", CopyDBPath); copyDatabase = NULL; } //source to copy // How in this area? } 

Is it correct? and how to do more? // source for copying area.

+6
source share
2 answers

in sqlite3 you can combine ATTACH [1] and CREATE TABLE .. AS [2] commands:

first you open the database "bDB", and then run the following statement:

 ATTACH DATABASE "myother.db" AS aDB; 

After that, you can use the CREATE TABLE syntax:

 CREATE TABLE newTableInDB1 AS SELECT * FROM aDB.oldTableInMyOtherDB; 

This will “copy” the data into your new database. If you want to combine data, there is also an INSERT statement [3], but you will need to refer to your fields as follows:

 INSERT INTO newtable (field1,field2) SELECT otherfield1,otherfield2 FROM aDB.oldTableInMyOtherDB; 

Literature:

[1] http://www.sqlite.org/lang_attach.html

[2] http://www.sqlite.org/lang_createtable.html

[3] http://www.sqlite.org/lang_insert.html

+21
source

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); } 
0
source

Source: https://habr.com/ru/post/915017/


All Articles