3

いくつかのデータベースを使用して辞書アプリケーションを作成しました。ブックマークに列を保存する以外はすべて正常に機能します。、私は知っています、NSBundleのファイルを変更することはできませんが、どうすれば修正できますか。ここにコードがありますので、私を助けていただければ幸いです。

- (NSString *) getDBPath {
    NSString *path;

    if ( [[[NSUserDefaults standardUserDefaults] stringForKey:@"Bv"] intValue] == 2)
    {
        path = [[NSBundle mainBundle]pathForResource:@"pr-eng" ofType:@"sqlite"];

    } else {

        path = [[NSBundle mainBundle]pathForResource:@"eg-pr" ofType:@"sqlite"];
    }

    if ( [[[NSUserDefaults standardUserDefaults] stringForKey:@"Bv"] intValue] == 3)
    {
        path = [[NSBundle mainBundle]pathForResource:@"german" ofType:@"sqlite"];
    }

    return path;    
}

これがブックマーク機能です:

-(void) setBookMark:(NSInteger)oid {

sqlite3_stmt    *statement;

const char *dbpath = [[self getDBPath] UTF8String];

if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString *SetFavSQL = [NSString stringWithFormat: @"UPDATE DIC SET bookmark=1 WHERE id=\"%d\"", oid];
    //      NSLog(@"%@",SetFavSQL);
    const char *SetFav_stmt = [SetFavSQL UTF8String];

    sqlite3_prepare_v2(database, SetFav_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) != SQLITE_DONE)
    {

    }
    sqlite3_finalize(statement);
    sqlite3_close(database);
}
}

データベースの読み取り:

-(void) read_Database{

NSMutableArray *DB_Array = [[NSMutableArray alloc] init];
NSMutableArray *word_Array = [[NSMutableArray alloc] init];

if(sqlite3_open([[self getDBPath]UTF8String], &database) == SQLITE_OK) {

    NSString *sql =@"SELECT * FROM DIC";

    //        NSLog(@"%@",sql);

    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK) {
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {


            NSInteger oid = sqlite3_column_int(compiledStatement, 0);

            const char* f1 = (const char*)sqlite3_column_text(compiledStatement, 1);
            NSString *oName = f1 == NULL ? nil : [[NSString alloc] initWithUTF8String:f1];

            const char* f2 = (const char*)sqlite3_column_text(compiledStatement, 2);
            NSString *oMean = f2 == NULL ? nil : [[NSString alloc] initWithUTF8String:f2];


            const char* f3 = (const char*)sqlite3_column_text(compiledStatement, 3);
            NSString *oPron = f3 == NULL ? nil : [[NSString alloc] initWithUTF8String:f3];

            NSInteger bm = sqlite3_column_int(compiledStatement, 5);

            readerClass = [[Reader alloc]initWithReadDB:oid Name:oName Mean:oMean Pron:oPron bookMark:bm];

            [DB_Array addObject:readerClass];
        }
    }
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);


AppDelegate *appDelegateClass = (AppDelegate *)[[UIApplication sharedApplication] delegate];

[appDelegateClass.wordList removeAllObjects];
appDelegateClass.wordList=[word_Array mutableCopy];


[appDelegateClass.dictionaryArray removeAllObjects];
appDelegateClass.dictionaryArray=[DB_Array mutableCopy];
}
4

3 に答える 3

2

私は知っています、NSBundleのファイルを変更することはできません

素晴らしい、少なくともあなたは問題をグーグルで検索する努力をしました。次に、アプリケーションの最初の起動時に更新するこれらのファイルを、NSDocumentsDirectoryなどの書き込み可能なパスにコピーしてみませんか?

NSArray *paths = NSSearchPathsForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:path toPath:[docsDir stringByAppendingPathComponent:@"db.sql"] error:NULL];

于 2012-09-22T16:52:34.323 に答える
0

マスターデータベースを1つだけ作成する必要があり、その中に複数のテーブルを含めることができると思います。それはあなたの問題を解決することができます。

于 2012-10-01T11:37:09.167 に答える
0

@ H2CO3の回答をさらに改善し、さらに明確にするために、回答を投稿します:)

iPhoneまたはiPad(iOS)のデータベースにデータを保存するために私が行うことは次のとおりです。すべてのデータはローカルでデバイスに保持されます。したがって、アプリがアンインストールされた場合、ユーザーがアプリのバックアップを取り、バックアップから復元されていない場合、すべてのデータが失われます。

NSFileManager *fileMgr;
NSString *homeDir;

これらは、以下の関数で使用するivarです。すべての関数は.mファイルにあり、更新、挿入、選択、削除など、データベースに関連するすべての関数が含まれています。

-(void)CopyDbToDocumentsFolder
{
    NSError *err=nil;
    fileMgr = [NSFileManager defaultManager];
    NSString *dbpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"res_in_mil.sqlite"];
    NSString *copydbpath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"res_in_mil.sqlite"];
    [fileMgr removeItemAtPath:copydbpath error:&err];
    if(![fileMgr copyItemAtPath:dbpath toPath:copydbpath error:&err])
    {
        UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"Unable to copy database." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [tellErr show];

    }
}
-(NSString *) GetDocumentDirectory
{
    fileMgr = [NSFileManager defaultManager];
    homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    return homeDir;
}

名前は自明なので、これらの機能が何のためにあるのかを説明する必要はありません。

そして、appDelegate.mで、以下のコードを使用して、DBを編集できる指定された場所にDBをコピーします。このコードは- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsメソッド内にあります。

NSFileManager *fileMgr;
myDB *dbObj = [[myDB alloc]init];
fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[dbObj GetDocumentDirectory] stringByAppendingPathComponent:@"res_in_mil.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
    [dbObj CopyDbToDocumentsFolder]; 

DBが1つしかないため、これを1つのDBに使用します。ただし、これらの関数を拡張して、複数のDBをコピーできると思います。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions上記のメソッドのコードを使用して、必要なDBがDocumentDirectoryにあるかどうかを識別するために、1つの関数を作成することもできます。

そして、挿入、更新、削除、選択操作については、以下のコードを使用してDBを開いてアクセスできるようにします。

fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[self GetDocumentDirectory] stringByAppendingPathComponent:@"res_in_mil.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];

if(!success)
    {
        NSLog(@"Cannot locate database file '%@'",dbPath);
        [self CopyDbToDocumentsFolder];
    }

ハッピーコーディング:)

于 2012-09-25T10:37:34.837 に答える