0

sqlite はインメモリ データベースを作成できますが、iPhone で行うことはできますか?

これは、sqlite http://www.sqlite.org/inmemorydb.htmlのドキュメントの状態です。

試しましたが、失敗します。データベースの作成は成功しましたが、テーブルの作成に失敗しました。以下は私のコードです:

-(BOOL) open{

    NSString *path = @"";
    if(sqlite3_open([path UTF8String], &database_) == SQLITE_OK) {
        //bFirstCreate_ = YES;
        NSLog(@"open == YES");
        [self createChannelsTable:database_];
        return YES;
    } else {
        sqlite3_close(database_);
        NSLog(@"Error: open database file.");
        return NO;
    }
    return NO;
}


- (BOOL) createChannelsTable:(sqlite3*)db{

    NSString *comment = [[NSString alloc] init];
    comment = @"CREATE TABLE Temp(Name text, Address text}";
    sqlite3_stmt *statement;
    if(sqlite3_prepare_v2(db, [comment UTF8String], -1, &statement, nil) != SQLITE_OK) {
        NSLog(@"Error: failed to prepare statement:create channels table");
        return NO;
    }
    int success = sqlite3_step(statement);
    sqlite3_finalize(statement);
    if ( success != SQLITE_DONE) {
        NSLog(@"Error: failed to dehydrate:CREATE TABLE channels");
        return NO;
    }
    NSLog(@"Create table 'channels' successed.");

    return YES;
}
4

1 に答える 1

0

SQLコマンド文字列にセミコロンがなく、')'ではなく'}'で終わっていない可能性がありますか?

comment = @"CREATE TABLE Temp(Name text, Address text}";

「住所テキスト}」の後にセミコロンが必要なので、次のようになります。

comment = @"CREATE TABLE Temp(Name text, Address text);";

NSStringの「コメント」を作成すると、メモリリークも発生すると思います。それを初期化してから、assignステートメントを使用したときに別の文字列を格納するようにコメントポインタに指示しました。

あなたは次のように1でそれらの2つのステップを行うことができます:

NSString *comment = [[NSString alloc] initWithString:@"CREATE TABLE Temp(Name text, Address text}";
于 2011-08-24T09:26:34.953 に答える