2009-11-10 124 views
1

我維護一個SQLite數據庫在我的iPhone應用程序,所以,我想知道如何可以堅持一個單一的版本和更新。iPhone的SQLite數據庫路徑問題

其表示,如果要進行更新的分貝ü應該將它複製到文件夾

在我的iPhone模擬器一樣,每次文件夾改變我打開應用程序

如果其複製並修改被做了..如何應用程序反映在我的應用程序包 db的更新,所以當應用程序再次啓動時,應用程序將數據庫再次從我的包複製到文檔文件夾,以便能夠對其進行更改..

我的問題是,有時我會進行更改並再次加載應用程序以找到在變化消失了!

所有我做了什麼:

  1. 創建使用某種路徑上和資源的終端在Xcode文件夾右鍵單擊添加現有文件
  2. 我這個每次做我加載應用程序的數據庫:
-(void)DBinit 
{ 
    // Setup some globals 
    databaseName = @"articlesdb.sql"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] copy]; 
    NSLog(databasePath); 
    // Execute the "checkAndCreateDatabase" function 
    [self checkAndCreateDatabase]; 
} 

-(void)checkAndCreateDatabase 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) 
    { 
     [fileManager removeItemAtPath:databasePath error:nil]; 
     return; 
    } 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

    fileManager release]; 
} 

回答

1

Xcode中,與iPhone模擬器相結合,爲您的應用,將間每個版本創建一個新的app文件夾將舊文檔文件夾及其內容覆蓋到新的應用程序文件夾中。您需要檢查文檔文件夾的內容,並確定您的Bundle中的文件是否適合用於文檔文件夾中的文件。你應該明智而不是輕率地做出這個決定。

這是正確的行爲。您的軟件的用戶可能對數據庫做了一些重大更改和修改。他們可能不喜歡它,如果升級一個小錯誤修復程序或甚至功能增強功能時,它們的數據庫更改,高分,修改後的圖像或應用程序保存的任何內容突然被替換爲通用版本。

你應該做的是比較新數據庫中的數據與用戶所擁有的數據,而對於SQLite,應該將用戶的條目導入或複製到新的數據庫中,然後可以移動將新數據庫移至文檔文件夾,刪除用戶的數據庫,但將其替換爲具有用戶數據和新數據的數據庫。

簡而言之:確保您不會用您的套件中的新數據庫替換現有的數據庫,因爲這會吹掉用戶的更改。

+0

感謝您的幫助和時間 我想要做的是在用戶對文檔文件夾中的數據庫進行更改之後。 我希望將它複製到數據庫中的數據庫中。 – 2009-11-10 10:02:58

+0

或自動發生 – 2009-11-10 10:03:59

+0

Bundle始終爲只讀。沒有東西可以被複制回來。這就是爲什麼你必須將數據庫複製到Downloads文件夾,否則數據庫是隻讀的。 – mahboudz 2009-11-10 11:41:20