How to use a pre-populated SQLite database using PhoneGap / Cordova 2.0?

I want to use a pre-populated database with my web application, so that my application works offline. How can I do this with the latest version of PhoneGap / Cordova (2.0)?

I know this question has been asked before, but all the answers seem outdated regarding the current version of cordova and iOS

https://github.com/atkinson/phonegap-prepopulate-db has not been updated for two years https://github.com/davibe/Phonegap-SQLitePlugin has not been updated for 7 months and is 1.7

I found a post here: http://www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap - is this the only way?

In addition, I should note that I use iOS 6

+6
source share
3 answers

I used the following plugin for Cordova 2.7 with iOS6 and I see that it has been recently updated. https://github.com/pgsqlite/PG-SQLitePlugin-iOS

They also have an Android version. Part of the problem is changing PhoneGap, so updating plugins often can take a lot of time, so they don't work. Here is the code for the init method in AppDelegate.m. I use in this project http://www.binpress.com/app/conference-core-ios-for-phonegap/1483

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

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

if __has_feature (objc_arc)

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

yet

  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 over BOOL success; // Create a FileManager object, we will use this to check the status // of the database and to copy it over if required NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if the database has already been created in the users filesystem success = [fileManager fileExistsAtPath:databaseFile]; // If the database already exists then return without doing anything if(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 package NSString *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 ...

+2
source

Thanks! I would not be able to do this without running here!

However, this did not work for me out of the box, so I updated it, and I'm not at all “good” with my own iOS code, so I am VERY open for suggestions.

I added the suggested code to the AppDelegate.m file, getting a bunch of errors. So, I went to the AppDelegate.h file:

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

Do it like this:

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

I just needed to declare variables there. I also deleted this line:

 [fileManager release]; 

How, according to this answer, would my project seemingly be automatically released? Hope

Now I have to work on getting the DB file to the right place for xcode to find it.

Hope this helps!

0
source

Although this question is old ... I thought it could help someone post my solution - found in 2017. (Since PhoneGap has changed, older solutions will no longer work or be accepted on the Android and Apple markets.) I have been working on this problem for a long time - the problem is that you want to import a pre-populated database that has no easy solutions. I found the best and simplest - and currently only ONLY - solution for LiteHelpers CORDOVA-SQLITE-EXT ( https://github.com/litehelpers/cordova-sqlite-ext )

I added this to my application and it works well. I had to disconnect from PhoneGap, although using CORDOVA, but it works for me.

0
source

All Articles