我的代码中有以下声明:
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, ¬eDB)==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“有人可以帮忙吗?
答案 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");
}
}