How to use a prepopulated SQLite database with PhoneGap / Cordova 2.0? How to use a prepopulated SQLite database with PhoneGap / Cordova 2.0? sqlite sqlite

How to use a prepopulated SQLite database with PhoneGap / Cordova 2.0?


I have used the following plugin for Cordova 2.7 with iOS6 and I see it was just updated a day ago. https://github.com/pgsqlite/PG-SQLitePlugin-iOS

They also have an Android version here as well.Part of the problem is PhoneGap changes so often keeping plugins up to date can be time consuming so they lapse. Here is the code for the init method in the AppDelegate.m I use in this project http://www.binpress.com/app/conference-core-ios-for-phonegap/1483

Keep in mind that you sometimes need to wipe the iOS Simulator so it reloads the files related to the application.

- (id)init{NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];int cacheSizeMemory = 8 * 1024 * 1024; // 8MBint cacheSizeDisk = 32 * 1024 * 1024; // 32MB

if __has_feature(objc_arc)

    NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];

else

    NSURLCache* sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];

endif

[NSURLCache setSharedURLCache:sharedCache];
databaseName = @"SomeCoolDatabase.db";NSLog(@"databaseName: %@", databaseName);databasePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];NSLog(@"databasePath: %@", databasePath);databaseFile = [databasePath stringByAppendingPathComponent:databaseName];NSLog(@"databaseFile: %@", databaseFile);// Execute the "checkAndCreateDatabase" function[self checkAndCreateDatabase];self = [super init];return self;

}

-(void)checkAndCreateDatabase {

// Check if the SQL database has already been saved to the users phone, if not then copy it overBOOL success;// Create a FileManager object, we will use this to check the status// of the database and to copy it over if requiredNSFileManager *fileManager = [NSFileManager defaultManager];// Check if the database has already been created in the users filesystemsuccess = [fileManager fileExistsAtPath:databaseFile];// If the database already exists then return without doing anythingif(success){    NSLog(@"Database Present");    return;} else {    NSLog(@"database not present");}// If not then proceed to copy the database from the application to the users filesystem// Get the path to the database in the application packageNSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];// Create the database folder structure[fileManager createDirectoryAtPath:databasePath withIntermediateDirectories:YES attributes:nil error:NULL];// Copy the database from the package to the users filesystem[fileManager copyItemAtPath:databasePathFromApp toPath:databaseFile error:nil];[fileManager release];

}

Hope this helps...


Thank you! I wouldn't have been able to do this without starting here!

That said, this wasn't working for me out of the box, so I updated it, and I'm by no means 'good' with native code for iOS, so I'm VERY open to suggestions.

I added the suggested code to the AppDelegate.m file, got a heap of errors.So, I moved over to the AppDelegate.h file:

@interface AppDelegate : NSObject <UIApplicationDelegate>{} --Looks like this to start.

Make it look like this:

@interface AppDelegate : NSObject <UIApplicationDelegate>{   NSString* databaseName;   NSString* databasePath;   NSString* databaseFile;}

I just had to declare the variables in there.I also removed this line:

[fileManager release];

As, according to this answer, my project seemingly automatically releases? I hope?

Now I have to work on getting the DB file into the correct location for xcode to find it.

Hope this helps!


Though this question is old... thought it might help someone to post my solution - found in 2017. (Since PhoneGap has changed, older solutions no longer will work or be accepted on the Android and Apple app markets.) I worked on this problem for a long time - the issue is you want to import a pre-populated database - which there are not alot of easy solutions for. I found the best and easiest - and currently just about the ONLY - solution to be LiteHelpers CORDOVA-SQLITE-EXT (https://github.com/litehelpers/cordova-sqlite-ext)

I have added this in to my app and it is working well. I had to switch away from PhoneGap though - and use CORDOVA - but it is working for me.