2010-09-17 66 views
1

我使用的是「著名的」 :)功能createEditableCopyOfDatabaseIfNeeded僅供如果沒有數據庫的副本,在iPhone應用程序創建我的數據庫的可編輯副本。該功能複製的文件目錄中的原始數據庫..很簡單.. 但現在我陷入困境,因爲我不知道爲什麼,但與SDK 3.2.3在iOS 4.1中的功能copy my db .... EMPTY !!!createEditableCopyOfDatabaseIfNeeded創建數據庫的空副本!

下面是代碼:

- (void)createEditableCopyOfDatabaseIfNeeded 
{ 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"funghi.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"funghi.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

的db是完全正確!我可以用firefox的sqlite插件讀取原始數據庫! 有什麼幫助嗎?

Thx很多!!!

回答

2

僅用於測試目的:

嘗試先刪除現有的數據庫。我猜測,當你在編寫代碼和調試時,你會在這個例程存在之前運行你的應用程序。 SQLite會爲你創建一個空的數據庫。

+0

或者直接從手機中刪除整個應用程序。 – 2012-01-14 14:16:36

相關問題