sqlite3_open - 无法打开数据库

时间:2013-10-10 17:27:18

标签: iphone ios sqlite

我的代码中有以下声明:

NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];

databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Notebook.sqlite"]];
NSFileManager *fileMgr = [NSFileManager defaultManager];
const char *dbpath = [databasePath UTF8String];
NSLog(@"open sqlite at %s",dbpath);
if (sqlite3_open(dbpath, &noteDB)==SQLITE_OK)

NSLog的输出是

  

/ Users / GinLin / Library / Application Support / iPhone Simulator / 7.0 / Applications / CE1BAB1C-3DEC-4A1D-A083-537B0B51C99D / Library / Documentation / Notebook.sqlite

但它似乎没有打开我的数据库。 我找不到“/ CE1BAB1C-3DEC-4A1D-A083-537B0B51C99D / Library /".

下的文件夹”Documentation“

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我认为您打算使用NSDocumentDirectory而不是NSDocumentationDirectory,例如:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];

答案 1 :(得分:0)

首先,您必须检查数据库路径是否包含de file,否则,复制数据库

NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString* resourcePath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];

    //Destination path
NSString *dbPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"database.db"];
    //Find file
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(success==YES){
    NSLog(@"File found");
}else{
    NSError* error;
    if(![fileMgr copyItemAtPath:resourcePath toPath:dbPath error:&error]){
        NSLog(@"Can't copy");
    }else{
        NSLog(@"That's all :D");
    }
}