【问题标题】:IPhone - create In-Memory DatabaseiPhone - 创建内存数据库
【发布时间】:2011-08-24 09:16:00
【问题描述】:

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;
}

【问题讨论】:

    标签: iphone database sqlite in-memory


    【解决方案1】:

    您是否可能在 SQL 命令字符串中缺少分号并且错误的结尾是 '}' 而不是 ')' ?

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

    在“地址文本}”之后需要一个分号,所以它变成:

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

    我认为您在创建 NSString“评论”时也会发生内存泄漏。初始化它,然后在使用 assign 语句时告诉注释指针存储另一个字符串。

    您可以像这样在 1 中执行这两个步骤:

    NSString *comment = [[NSString alloc] initWithString:@"CREATE TABLE Temp(Name text, Address text}";
    

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 2010-09-23
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2015-08-27
      • 2012-02-28
      • 1970-01-01
      • 2012-03-22
      相关资源
      最近更新 更多