0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"idataBase.sqlite3"];
NSLog(@"%@",databasePath);
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"idataBase" ofType:@"sqlite3"];
NSLog(@"%@",databasePathFromApp);
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
if (!databaseAlreadyExists){
    [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

I tried to copy the database to document directory, But it returns null when i try to get the database path from main bundle and my app crashed. The database is present in app. But why it returns null?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Which is `nil`? `databasePath` or `databasePathFromApp`? – rmaddy Feb 02 '14 at 04:32
  • databasePathFromApp its nil. – Rashad Feb 02 '14 at 04:36
  • Then the app's resource bundle does not actually contain a file named `idataBase.sqlite3`. Make sure the file is targeted properly and the filename has that exact name (including case). – rmaddy Feb 02 '14 at 04:38
  • I am facing some weird problem, I solve the above problem by adding the database file again. But now I can not insert data in database. – Rashad Feb 02 '14 at 04:39
  • possible duplicate of [NSBundle returns nil](http://stackoverflow.com/questions/3608683/nsbundle-returns-nil) – codercat Feb 02 '14 at 04:42
  • http://stackoverflow.com/questions/19350947/pathforresource-returns-null-for-plist – codercat Feb 02 '14 at 04:43
  • Not being able to write to the database could be caused by dozens of things. Post the code, errors, etc. in another question if you can't figure it out. – Larry Feb 02 '14 at 04:57

1 Answers1

2

if you have twice .sqlite3 in your project

because may be your xCode target is "Build Phases" add the file under "Copy Bundle Resources"".

codercat
  • 22,873
  • 9
  • 61
  • 85