views:

89

answers:

2

Hello,

I want to initialize my app with a small sqlite3 DB. I tried to put it in the Documents directory, but sometimes it copies it with zero size and sometimes it doesnot copy it at at all.

How sholud I initialize my app with a DB full of info?

Thankyou

+2  A: 

How do you copy it with your application?
If you ship your application with DB inside app bundle (as a resource) you should copy it to the documents folder from resources if it is absent and work with it there:

NSFileManager *fileManager = [[NSFileManager defaultManager] autorelease];
BOOL exists = [fileManager fileExistsAtPath:databasePath];

if (exists) {
    return;
}

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
Vladimir
A: 

Thank you Vladimir,

This was helpful. I made a few changes and here is the final result that works great:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *databasePath = @"personalLMS.s3db";

NSFileManager *fileManager = [[NSFileManager defaultManager] autorelease];

NSString * databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"personalLMS" ofType:@"s3db"];

[fileManager copyItemAtPath:databasePathFromApp toPath:[documentsDirectory stringByAppendingPathComponent:databasePath] error:nil];

Meir