0

我的代码中有以下语句:

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/”下找不到文件夹“文档”。

有人可以帮忙吗?

4

2 回答 2

0

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

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");
    }
}
于 2013-10-10T17:43:28.067 回答
0

我相信您打算使用NSDocumentDirectorynot NSDocumentationDirectory,例如:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
于 2013-10-10T17:42:41.703 回答