Meir
Meir

Reputation: 1054

Adding file to Documents Directory (or elsewhere)?

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

Upvotes: 2

Views: 975

Answers (2)

Meir
Meir

Reputation: 1054

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];

Upvotes: 1

Vladimir
Vladimir

Reputation: 170839

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];

Upvotes: 4

Related Questions