简体   繁体   中英

Error while creating db in objective-c

I am creating an app in IOS with cordova 2.1.0 framework. I am creating sqlite3 db in objective-c with following code:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.databaseFile = [docDir stringByAppendingPathComponent:@"splistdb.sqlite3"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;


if (![fileManager fileExistsAtPath:self.databaseFile]) {
    /*[fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"FriendsDatabase" ofType:@"sqlite3"] toPath:dbFileName error:&error];*/
    NSString *path = [[NSBundle mainBundle] pathForResource:@"splistdb" ofType:@"sqlite3"];
    NSLog(@"doc path=%@",path);
    [fileManager copyItemAtPath:path toPath:self.databaseFile error:&error];
    [self createConfigTable];
    NSLog(@"database created");
} else {
    NSLog(@"fail to create database");
}

I have included libsqlite3.dylib file in build phases heading. But i am getting following error:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'

What could be wrong in my code? Thanks.

Your error message says that path is nil , probably because the file splistdb.sqlite3 is not being copied into the application bundle. Make sure that it is in your project, and has been added to your target application.

最有可能这行给你零价值

NSString *path = [[NSBundle mainBundle] pathForResource:@"splistdb" ofType:@"sqlite3"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM