2

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

Meir
  • 994
  • 12
  • 25

2 Answers2

4

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
  • 170,431
  • 36
  • 387
  • 313
1

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];
Oleg Trakhman
  • 2,082
  • 1
  • 17
  • 35
Meir
  • 994
  • 12
  • 25