简体   繁体   English

iPhone-创建内存数据库

[英]IPhone - create In-Memory Database

sqlite is able to create in-memory database, but is it possible done in iPhone? sqlite能够创建内存数据库,但是可以在iPhone中完成吗?

This is the document state that in sqlite http://www.sqlite.org/inmemorydb.html 这是sqlite http://www.sqlite.org/inmemorydb.html中的文档状态

I tried, but fail. 我尝试过,但是失败了。 It successful create the database, but fail to create table. 它成功创建数据库,但无法创建表。 Below is my code: 下面是我的代码:

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

Are you possibly missing a semicolon in your SQL command string and wrong ending '}' instead of ')' ? 您是否可能在SQL命令字符串中丢失了分号,并且以'}'而不是')'结尾的错误?

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

Needs a semicolon after "Address text}" so it becomes: 在“地址文本”之后需要一个分号,因此它变为:

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

I think you also go a memory leak when you create the NSString "comment". 我认为您在创建NSString“ comment”时也会发生内存泄漏。 You init it then you told the comment pointer to store another string when you used the assign statement. 初始化它,然后在使用assign语句时告诉注释指针存储另一个字符串。

You can do those two step in 1 like so: 您可以像这样在第一步中完成这两个步骤:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM