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