Pages

Showing posts with label SQlite. Show all posts
Showing posts with label SQlite. Show all posts

Saturday, November 5, 2011

How to attach database with other database in SQLite

To Attach DB with another DB, i use below code................


Create first database:
[self createEditableCopyOfDatabaseIfNeeded];
Create second database:
[self createEditableCopyOfDatabaseIfNeeded_New];

Attach Database second with first:
[self AttachDB];
use of both data base in single SQL:
[self inserttblData];

-(voidAttachDB
{
               BOOL success;
               NSFileManager *fileManager=[[NSFileManager defaultManager]autorelease];
               NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        
               success = [fileManager fileExistsAtPath:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"database2.sqlite3"]];
               if(success)
               {
                        NSString *sql = [NSString stringWithFormat:@"ATTACH DATABASE \"%@\" AS newdb",[[paths objectAtIndex:0] stringByAppendingPathComponent:@"database2.sqlite3"]];
                        // here database is an sqlite object and which refer database1
                        int intReturnValue = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);       
                        if(intReturnValue == 1)
                         {
                                    NSAssert1(0, @"Error: failed to attach database with message '%s'.", sqlite3_errmsg(database));
                        }
                        else
                        {
                                    NSLog(@"Done");  
                        }
               }   
}

-(int)inserttblData
{
               int i=0;
               @try
               {
                       
                        sqlite3_stmt *statement=nil;
                        NSString  *sql=nil;  
                        sql=[NSString stringWithFormat:@"INSERT INTO testtable1 SELECT * FROM newdb.testtable2"];
                        if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK)
                        {
                                    NSAssert1(0, @"Error: failed to insertstatement with message '%s'.", sqlite3_errmsg(database));
                        }                                  
                        int success=sqlite3_step(statement);
                        if (success == SQLITE_ERROR)
                        {
                                    NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
                        }
                        sqlite3_finalize(statement);
                       
                        return i;
                       
                       
               }
               @catch (NSException *e)
               {
                        return 0;
               }
               return i;
}

If you got any new way and get problem, write here.......

Thx,