1

I am quite new to iOS app development.

I am trying to build a sample DB app which saves records to DB and fetches from it.

The App works well when I test with simulators. I have created a local database and also I am programatically making a copy of it when required. Also I have checked that local DB is referenced in the 'Copy Bundle Resources'.

But the error I am getting is, " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' ".

I think the piece of code which causes this problem is " [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; "

It work perfectly good for simulators but not when I test with my device.

Expecting help.

My code is,

- (id)init {

    self = [super init];

    //Datbase Name
    DBName=@"Person.sqlite3";

    //Getting DB Path
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentsPath objectAtIndex:0];

    DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
    //DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
    NSLog(@"DBPath is  %@",DBPath);

    return self;

}


-(void)checkAndCreateDB{

    BOOL Success;

    //NSFileManager maintains File
    NSFileManager *FileManager = [NSFileManager defaultManager];
    NSLog(@"line 1");

    //Checks Database Path
    Success = [FileManager fileExistsAtPath:DBPath];
    NSLog(@"line 2");

    //If file exists, it returns true
    if(Success)return;
    NSLog(@"line 3");

    //NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];

    // Get the path to the database in the application package
    NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];
    NSLog(@"line 4");

    [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
    //[FileManager release];
    NSLog(@"line 5");

}

My Application crashes while testing in device after the line NSLOG(line 4); But works good in simulator.

Many Thanks Prakash

Niklas
  • 13,005
  • 23
  • 79
  • 119
suj
  • 507
  • 1
  • 8
  • 22

2 Answers2

0

The device can not write to the app bundle, you need to use the Documents directory.

Here is how to get the Documents directory path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

If you have an initial db in the app bundle copy it to the Documents on initial startup.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks for a quick reply :) My existing code does exactly the same and also my local db is in Documents. NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentsPath objectAtIndex:0]; DBPath= [documentsDir stringByAppendingPathComponent:DBName]; But still it doesn't work in device. – suj Jan 16 '12 at 03:24
0

change this line

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];

to

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"sqlite3"];

also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation

X Slash
  • 4,133
  • 4
  • 27
  • 35