2013-10-10 60 views
0

我在我的代码下面的语句:sqlite3_open - 无法打开数据库

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输出

/用户/ GinLin /库/应用程序支持/ iPhone模拟器/ 7.0 /应用/ CE1BAB1C-3DEC-4A1D-A083-537B0B51C99D /库/文档/ Notebook.sqlite

,但它似乎并没有被打开我的数据库。 而我无法在“/ CE1BAB1C-3DEC-4A1D-A083-537B0B51C99D/Library /”下找到文件夹“文档”。

任何人都可以帮忙吗?

回答

0

我相信你的意思是使用NSDocumentDirectoryNSDocumentationDirectory,例如:

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

是的!有用!非常感谢你 – Zheng

0

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

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"); 
    } 
}