2011-06-11 125 views
0

我正在模擬器中正常工作的數據庫應用程序(插入,更新,刪除所有功能正確)。嘗試在iPhone上安裝/運行應用程序時,我的問題就開始了。在設備上測試sqlite數據庫應用程序

在初次運行時,程序在應用程序的documents目錄中創建數據庫的可編輯版本。在應用程序包中未找到數據庫時,我的測試失敗。數據庫已被添加到XCode的Resources文件夾中。

檢查數據庫(加上幾個的NSLog語句)的現有版本,以及創建一個,如果一個不存在如下所述的代碼:

- (的NSString *)createWriteableDatabase { BOOL databaseExists; NSError *錯誤;是

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *writeableDB = [documentsDir stringByAppendingPathComponent:@"flashCard.db"]; 

NSLog(@"writeableDB = '%@'.", writeableDB); 

databaseExists = [fileManager fileExistsAtPath:writeableDB]; 

NSLog(@"databaseExists = %d.", databaseExists); 

if (databaseExists) 
    return writeableDB; 

//--- Writeable version of database does not exist -- copy from bundled software. --- 

NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] 
         stringByAppendingPathComponent:@"flashCard.db"]; 

NSLog(@"defaultPath = '%@'.", defaultPath); 

databaseExists = [fileManager fileExistsAtPath:defaultPath]; 

NSLog(@"databaseExists = %d.", databaseExists); 

databaseExists = [fileManager copyItemAtPath:defaultPath toPath:writeableDB error:&error]; 

if (!databaseExists) 
{ 
    NSAssert1(0, @"Failed to create writeable database file: '%@'.", [error localizedDescription]); 
    return @""; 
} 

return writeableDB; 

}

在調試器顯示的消息如下所示:

警告:無法讀取符號/的XCode 3.2.5 /平臺/ iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/System/Library/AccessibilityBundles/AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader(找不到文件)。寫入數據='/var/mobile/Applications/9DBC50B5-2559-4E6A-BD21-B5B56D9DA18A/Documents/flashCard.db'。 2011-06-10 21:12:11.144 FlashEm [3768:707] databaseExists = 0。 2011-06-10 21:12:11.149 FlashEm [3768:707] defaultPath ='/ var/mobile/Applications/9DBC50B5- 2559-4E6A-BD21-B5B56D9DA18A/FlashEm.app/flashCard.db」。 2011-06-10 21:12:11.154 FlashEm [3768:707] databaseExists = 0. 2011-06-10 21:12:11.169 FlashEm [3768:707] * - [DBAccess createWriteableDatabase]中的斷言失敗,用戶/ marathoner1234/Projects/FlashEm/Classes /../ DBAccess.m:92 2011-06-10 21:12:11.201 FlashEm [3768:707] *由於未捕獲異常'NSInternalInconsistencyException',reason:'終止應用程序無法創建可寫入的數據庫文件:'操作無法完成。無此文件或目錄'。'

在此先感謝您提供的任何幫助,如果我的問題文本格式不正確,我很抱歉 - 這是我第一次在此網站上提出問題。

(我看了看在同樣的問題發生的另外兩個問題,並檢查,看看他們是否已經應用到我的情況相同的問題,但他們沒有。)

+0

你能發佈你的包中的數據庫文件的實際名稱嗎? – 2011-06-11 05:11:11

+0

你有沒有在你的項目中保存數據庫文件的同時檢查了拷貝,也檢查了它。它沒有在設備中找到數據庫,因爲它沒有獲取數據庫。在模擬器中它的工作正常,因爲它有參考。你可以也檢查其他問題。那只是我的想法 – Sabby 2011-06-11 06:00:48

+0

Resources文件夾中顯示的文件名是flashcard.db。檢查「將項目複製到目標組的文件夾(如果需要)」後添加了文件。我已經刪除了該文件並重新複製了它,但無濟於事。 – Steve 2011-06-11 10:29:20

回答

0

您的崩潰報告說,你的數據庫文件找不到您的文檔目錄。只有當你的資源文件夾內沒有正確安裝數據庫文件意味着你的包內部發生這種情況,所以iPhone設備上的程序無法讀取你的數據庫文件。

+0

正如我上面提到的,數據庫被添加到XCode中的資源文件夾。否則,它將無法在模擬器中正常運行。 – Steve 2011-06-11 06:10:29

+0

我不允許回答我自己的問題,但是我發現了我的錯誤:我的角色是膽怯,骨頭錯誤 - 如果它是響尾蛇,它會在幾年前咬傷我。代碼中指定的文件名是flashCard.db(大寫'C'); Resources文件夾中的文件名是flashcard.db(小寫'c')。在代碼中更改了文件名,解決了問題。 謝謝所有添加了評論的人。 – Steve 2011-06-11 11:02:18

相關問題