Copy existing sqlite database into PhoneGap iOS application

Introduction:
Usually PhoneGap supports SQLite database to interact locally. There might be a scenario where we need to create the database freshly or sometimes need to use the existing database. The idea of writing this blog came to my mind while encountered a question in stackoverflow site.  In this blog we will focus on importing existing database into our PhoneGap application and use the same to interact with our application.

Description:
Lets have a pre-populated database having tables with data which needs to be imported to our application. It will help us to minimize our effort in creating the database freshly to the application on its startup. For this we need to follow the bellow steps.

Step 1 : Remove the .sqlite extension from the sqlite database. Ex. If we have the database called DummyDB.sqlite make it only DummyDB.

Step 2 : Then drag the DummyDB file to Resource directory in Xcode by choosing the option “Create groups for any added folder” and check the “Copy items into destination group’s folder (if needed)” option while adding reference.

Step 3 : Now use the following code to copy the database into the Document directory inside our application.

(void) copyDatabase{

 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSError *error;

 NSString *dbPath = [self getDBPath];
 BOOL success = [fileManager fileExistsAtPath:dbPath];

 if(!success) {

 NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DummyDB"];
 success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

 if (!success)
 NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
 }
}

- (NSString *) getDBPath
{

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
 NSString *documentsDir = [paths objectAtIndex:0];
 return [documentsDir stringByAppendingPathComponent:@"DummyDB"];
}

Add this code in AppDelegate.m file that we can find inside the Classes folder inside our application. Add this code just before @end line in AppDelegate.m file.

Here NSSearchPathForDirectoriesInDomains takes 3 parameters to search for the standard documents.

NSDocumentDirectory : Searching the documents directory .

NSUserDomainMask : Searching the users directory.

YES: User permission to search the directories.

Step 4 : Use this code [self copyDatabase]; and call this in the didFinishLaunchingWithOptions method like this

(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
 CGRect screenBounds = [[UIScreen mainScreen] bounds];

 [self copyDatabase];

Here we can find the copied database inside the Documents folder in the following directory.

~/Library/Application Support/iPhone Simulator/[OS version]/Applications/[appGUID]/

Step 5 : In this step we can open the copied database by using SQLite plugin. To integrate the SQLite plugin in our application we can follow the link bellow .

Integrate SQLite plugin in PhoneGap iOS application

Now open the database using following code.

function openDatabase() {
        try{
            if(!!window.openDatabase){
                var openDB = window.sqlitePlugin.openDatabase({name : "DummyDB"});
            }else{
                navigator.notification.alert("Local database is not supported by your device.");
            }
        }
        catch(error){
             console.log("ERROR:" + error.message);
        }

Summary:
So using the above example we can easily import the existing SQLite database in our PhoneGap application which in turns makes coding easy.

Written By: Sourabha Kumar Sahoo, Software Developer, Mindfire Solutions

2 thoughts on “Copy existing sqlite database into PhoneGap iOS application

Leave a comment