Indra
Indra

Reputation: 548

Open two sqlite database and execute query in iphone

Here i want to execute query using two database and copy data from table of database1 to another database2. here i am able to open one database but getting problem in openning other database. Thanks in advance to all.

CurrentStatus *status = [CurrentStatus sharedCurrentStatus];
    sqlite3 *database;
    sqlite3 *database1;
    sqlite3_stmt *statement;
    sqlite3_stmt *statement1;
    NSString *dbPath = [status.applicationDocumentDirectory stringByAppendingPathComponent:@"database.sqlite"];
    NSString *dbPath1 = [status.applicationDocumentDirectory stringByAppendingPathComponent:@"database1.sqlite"];

    if ((sqlite3_open_v2([dbPath UTF8String], &database, SQLITE_OPEN_READWRITE , NULL) == SQLITE_OK) && (sqlite3_open_v2([dbPath1 UTF8String], &database1, SQLITE_OPEN_READWRITE , NULL) == SQLITE_OK)) {

        NSString *sqlStr = [[NSString alloc] initWithFormat:@"select * from Login" ];
         NSString *sql = [[NSString alloc] initWithString:sqlStr];

                if ((sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK)) {
                    NSLog(@"DB prepare_v2 Opening successfully");
                    if (sqlite3_step(statement) == SQLITE_ROW) {

                    }
                    else
                    {

                    }
                    sqlite3_finalize(statement);
        NSLog(@"DB Opening successfully");

               sqlite3_close(database);
    }else
    {
        NSLog(@"else DB Opening successfully");
    }
        sqlite3_close(database);
    }

Upvotes: 3

Views: 854

Answers (3)

Kumar Aditya
Kumar Aditya

Reputation: 1097

You need to You need to Attach databases and then copy tables from one database to another database. You need to do something like this:

NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS %@", sourceDBPath,dbAlias];

if (sqlite3_exec(db, [attachSQL UTF8String], NULL, NULL, &errorMessage) != SQLITE_OK)
   {
        NSString *errorStr = [NSString stringWithFormat:@"The database could not be attached: %@",[NSString stringWithCString:errorMessage encoding:NSUTF8StringEncoding]];  
        return errorStr;
   }

After that you need to copy table , like:

NSString *queryString=[NSString stringWithFormat:@"CREATE TABLE %@ AS SELECT * FROM %@.%@;",newTableName,dbAlias,origTableName];

And you are done, you have the new copy of database.

Upvotes: 1

Kumar KL
Kumar KL

Reputation: 15335

Try something similar to achieve your task ..

First, you are opening the "DB1" database, and then execute the following statement:

ATTACH DATABASE "2ndDB.db" AS 2ndDB;

After that, you can use the CREATE TABLE syntax:

CREATE TABLE newTableInDB1 AS SELECT * FROM 2ndDB.oldTableInMyOtherDB;

This will "copy" the data over to your new database

Upvotes: 2

TamilKing
TamilKing

Reputation: 1643

Copy which are the data you want it from database1 add each row in NSMutableDictionary and then add it to NSMutableArray. Close and reset your statement then open the database2 copy the data from NSMutableArray, that array act as database for you..

I hope it will work for you..

Upvotes: 1

Related Questions