2012-05-06 18 views
5

Voglio copiare la tabella da aBB ad un altro bDB.Copia tabella in un db ad un altro db

Così ho fatto un metodo. Penso che aprire il database 2 e Usare la query di inserimento funzionerà ma non conosco il modo dettagliato.

-(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? 


} 

È giusto? e come fare di più? // sorgente per copiare l'area.

risposta

21

in sqlite3, è possibile combinare la ALLEGARE [1] e CREATE TABLE .. AS [2] comandi:

prima, si sta aprendo il database "BDB", e quindi eseguire la seguente dichiarazione:

ATTACH DATABASE "myother.db" AS aDB; 

Dopo di che, è possibile utilizzare la sintassi CREATE TABLE:

CREATE TABLE newTableInDB1 AS SELECT * FROM aDB.oldTableInMyOtherDB; 

questo sarà "copiare" i dati sopra al vostro nuovo database. Se si desidera unire i dati, c'è anche un INSERT [3] dichiarazione, ma con che sarà necessario fare riferimento i vostri campi come questo:

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

Riferimenti:

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

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

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

+1

+1 per ricordarmi la creazione delle tabelle veloce e copiare con "CREATE TABLE newTableInDB1 AS SELECT * FROM aDB.oldTableInMyOtherDB;" –

+0

ben spiegato. sarebbe fantastico se tu potessi scrivere in codice concreto ios objc. – OMGPOP

+0

I comandi sono SQL puri, li esegui con una chiamata a sqlite3_exec(). – Zuppa

0

Dopo ore di str uggle su SO, e con l'aiuto di post sopra, Finalmente sono stato in grado di creare il codice Objective-C per farlo.

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

Non dimenticare di scollegare il database con DETACH DATABASE phase2_db, nel caso in cui non lo si limitasse a chiuderlo. – Zuppa

Problemi correlati