我正在使用XCode 6.4和iOS Si,mulator iPhone 6.我的應用程序數據庫或表的每次運行都被重新創建,因此我丟失了以前插入的所有數據。我有1個數據庫裏面有1個表。數據庫文件位於項目的Supporing Files目錄中。這裏是數據庫初始化代碼:iOS模擬器重新創建sql數據庫/表
BOOL success;
NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"signals" ofType:@"sql"];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if (success) {
NSLog(@"exists");
return;
} else {
NSLog(@"No file found");
[fileManager copyItemAtPath:databasePath toPath:databasePath error:nil];
}
在運行 - 成功變量是YES - 所以我看到日誌「存在」。之後,我插入一個項目到我的表,並做選擇查詢,返回插入原始這一個。在下一次運行中,我應該看到兩行,但只有一行 - 所以我假設表被重新創建。我需要這個問題的幫助。
什麼是'[文件管理copyItemAtPath:databasePath toPath:databasePath錯誤:零];'怎麼辦呢?你只是錯過了從你的應用程序包複製數據庫文件到你的應用程序的文檔目錄(它保留了應用程序更新) – luk2302