2012-04-30 83 views
12

我創建了一個SQL數據庫使用「SQLite數據庫瀏覽器」,拖放到我的Xcode項目,並建立了應用程序。它的工作原理非常清楚的模擬器,但崩潰在iPhone上,與此錯誤:應用程序崩潰在數據庫創建嘗試

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Failed to create writable database file with message 'The operation could‚ 
not be completed. (Cocoa error 260.)'.' 

這裏是我的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Creates a writable copy of the bundled default database in the application Documents directory: 
    NSLog(@"AppDelegate...Looking for embedded Database file..."); 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    // Grab the path to the Documents folder: 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"]; 

    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) { 
     NSLog(@"Database File Exists in Documents folder!"); 
     NSLog(@"Its path is: %@", writableDBPath); 
     return YES; 
    } 
    else { 
    // But if the writable database does not exist, copy the default to the appropriate location. 
    NSLog(@"!!NO Database File Exists in Documents folder!"); 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"users.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
    else 
     NSLog(@"WROTE THE DATABASE FILE!!!"); 
} 

return YES; 
} 

同樣,這部作品在模擬器上,而不是在iPhone上。 (這不可能與文件有一個「.sql」擴展名而不是「.sqlite」擴展名有關,可以嗎?因爲這是「SQLite數據庫瀏覽器」給它創建的文件的擴展名。 。)

+0

據我所知,Cocoa錯誤260是NSFileReadNoSuchFileError,所以可能是因爲擴展。 – Vin

+0

好吧,我只是改變了文件名以「.sqlite」結尾 - 並將文件重新導入Xcode,並更改爲代碼以匹配新文件名 - 它仍然崩潰 - 同樣的原因。所以不是這樣...... – sirab333

+0

嘗試運行你在設備上使用的教程的代碼,看看是否有效。如果是這樣,db文件本身可能有問題。 – Vin

回答

28

答案與確保SQL文件的「目標會員」做正確設置,使項目「看到」它:

1)單擊SQL文件中的左Xcode中的-pane

2)打開/顯示文件檢查器(右側窗格)

3)在 「目標會員」,確保 「檢查」 是 「檢查」

就是這樣。

在我的問題
+0

謝謝,很好! – iDhaval

+0

@ sirab333你應該接受它作爲答案。 – Naeem

+0

@ sirab333:謝謝..這對我很有用! –

0

: 檢查該行:

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"] 

檢查文件名,在你的包和碼 - 錯誤也套管不同的文件名。

2

enter image description here

該解決方案可以解決我的問題,我希望它會幫助你的。

相關問題