0

Sqlite3データベースを使用するアプリを開発しています。今のところ、dbはアプリパッケージ内にありますが、実際のiPhoneで実際にテストすると、データを更新する際に問題が発生することはわかっています。

1-バンドルフェーズで、プロジェクトデータベースをライブラリフォルダーにコピーするにはどうすればよいですか?

2-このコピーされたデータベースにアクセスするにはどうすればよいですか?今のところ私のコードは次のとおりです。

NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDatabase.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];

何を変更する必要がありますか?

3-アプリを更新した場合、このデータベースを(ユーザーのデータとともに)保存するにはどうすればよいですか?

前もって感謝します!;)

4

1 に答える 1

1

データベースをResourcesフォルダーからDocumentsフォルダーにコピーする必要があります

sqlite3 *database;
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Lists.db"];
success = [fileManager fileExistsAtPath:writableDBPath];
// The writable database does not exist, so copy the default to the appropriate location.
if(!success)
{
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Lists.db"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        TFLog(@"Failed moving database... %@",[error localizedDescription]);
        return;
    }
}

ドキュメントフォルダ内のアプリデータベースを更新すると、状態を確認するだけで済みます。

     success = [fileManager fileExistsAtPath:writableDBPath];
if it does not exits new database will be copied their.
Just keep in mind database structure is same as before.
于 2012-08-22T06:19:51.903 に答える