Reputation: 1172
I've an existing SQLite database called data_items.sqlite. This database includes about 10 tables and some initial data which I would like to import into XCode to open the database with the Phonegap Plugin in this way:
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase("data_items.sqlite", "1.0", "PhoneGap Demo", 200000);
...
}
How can I import the datafile? Where do I have to copy the file?
Upvotes: 2
Views: 3568
Reputation: 445
First create database as u used name data_items.sqlite and remove .sqlite extension. Then drag this db at Resources directory in xCode and follow next .
Choose option for adding file box will appear check checkbox says 'Copy items into destination group's folder (if needed)' and finish.
You will see the Database file as sub list of Resources Directory
Sorry I don't have enough reputation to post images here
Now follow the link to copy this database file into location
How to copy sqlite database when application is launched in iOS?
I used the following code before the @end at last line in AppDelegate.m
- (void) copyDatabaseIfNeeded{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
_dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:_dbPath];
if(!success){
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data_items"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:_dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}}
/********* Database Path *********/
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"data_items"];
}
And Copy the line
[self copyDatabaseIfNeeded];
after the code
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
That's it. Use your code
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase("data_items", "1.0", "data_items", 200000);
...
}
You can also edit database directly in from the copied location. Use following link
Where does the iPhone Simulator store its data?
Upvotes: 2
Reputation: 180020
It appears not to be possible to access the assets folder from PhoneGap, so you have to write a native plugin to copy the database file:
Prepopulate SQLite DataBase in PhoneGap Application
Upvotes: 0