2016-05-23 1 views
0

私は、SQLiteデータベースの変更のために、私のアプリケーションの次のリリースのためにSQLiteデータベースに変更を加えた状況があります。主にクエリメソッドに列を追加します。ドキュメントディレクトリ内のファイルを置換する

次回ユーザーがアプリケーションを更新したときに変更されたため、新しいデータベースが必要です。以前のバージョンの私のアプリケーションでは、File.sqliteという名前のデータベースがありました。ユーザーがアプリケーションを新しくダウンロードしたときにそのファイルがドキュメントフォルダに保存されます。今、私は新しいファイルで同じ名前のFile.sqliteを構築しましたが、その中にいくつかのカラムなどがありますが、新しいファイルが古いファイルを置き換えないアプリケーションを更新します。

ユーザーがアプリケーションを更新したときにファイルが置き換えられるようにするにはどうすればよいですか?

は、今の時点で私は、私はドキュメントディレクトリに古いファイルのパスを取得するには、次の使用できることを理解:別の名前を持っている必要があり

//Reference the path to the documents directory. 
NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
//Get the path of old file. 
NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"]; 

は、新しいファイルをして、私はちょうど参照しますコードの新しい名前ですか?それは私にはばかげているようだ、私は古いのコピーを作成し、新しいバージョンを使用する必要がありますか?

また、これが最善のアプローチであるかどうかもわかりませんが、あなたがより良い方法を知っていれば助言してください。

+0

確かにあなたの答えは[これらの検索結果](http://stackoverflow.com/search?q=%5Bios%5D+sqlite+schema+change)にあります。 – rmaddy

+0

@rmaddy私は解決策を見つけることができたら私の質問をしなかったでしょう、私はそれらの解決策を見てみました。 –

+0

[this one](http://stackoverflow.com/questions/989558/best-practices-for-in-app-database-migration-for-sqlite?lq=1)、[this one](http: /stackoverflow.com/questions/10136256/updating-new-version-to-app-store-with-different-sqlite-db-structure)、[this one](http://stackoverflow.com/questions/16284430/how -to-overwrite-a-sqlite-db-in-ios)、および[this one](http://stackoverflow.com/questions/17369603/how-to-detect-that-the-app-version-has-変更/ 17375294?s = 3 | 0.7471#17375294)。 – rmaddy

答えて

1

アプリケーションが新規インストールかAppDelegateからのアップデートかを確認できます。しかし、あなたはNSUserDefaultsにあなたのAppバージョンを保存していないようだ、それはあなたのために少しトリッキーだろう。 File.sqliteの新しいバージョンをResourceに保存し、ドキュメントディレクトリにコピーします。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSString* curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 
    NSString* lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastVersion"]; 

    if (lastVersion == nil) 
    { 
     //App Installed for the first time. In your case it might be an update because you do not store lastVersion value. 

     //Reference the path to the documents directory. 
     NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
     //Get the path of old file. 
     NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"]; 

     if([[NSFileManager defaultManager] fileExistsAtPath: filePath]) 
     { 
      [fileManager removeItemAtPath:filePath error:&error] //Delete old file 
     } 
     //Copy New File.sqlite from Resource Bundle. 
     NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"sqlite"]; 
     [fileManager copyItemAtPath:resourcePath toPath:filePath error:&error]; 
    } 
    else if (![lastVersion isEqual: curVersion]) 
    { 
     // App is updated. For future Versions you can Update files from here! 
    } 

    [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:@"LastVersion"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

1
NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0]; 
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"File.sqlite"]; //File.sqlite old database 

NSString *pathStr = [documentsDirectory stringByAppendingPathComponent:@"File1.sqlite"]; // New database file name 
FMDatabase *database = [FMDatabase databaseWithPath:dbPath]; 
[database open]; 
FMDatabase *database1 = [FMDatabase databaseWithPath:pathStr]; 
[database1 open]; 

NSString *str = @"select * from table_name"; // old database fire query for use data value fetch 

FMResultSet *res=[database executeQuery:str]; 

// Your old database value updated to update new database. 
while ([res next]) { 
    NSString *query= // Write update query for update column value 
    [database1 executeUpdate:query]; 
} 
[database1 close]; 
[database close]; 

// remove old database file 
if ([[NSFileManager defaultManager] isReadableFileAtPath: dbPath]) 
{ 
    if ([[NSFileManager defaultManager] removeItemAtPath:dbPath error: NULL] != YES) 
     NSAssert2(0, @"Fail to copy database from %@ in %@", dbPath, pathStr); 
} 
関連する問題