iPhone - 创建内存数据库

发布于 2024-12-01 06:53:07 字数 1162 浏览 4 评论 0原文

sqlite 能够创建内存数据库,但是可以在 iPhone 中完成吗?

这是sqlite中的文档状态 我尝试过,但

失败了。创建数据库成功,但建表失败。 下面是我的代码:

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

sqlite is able to create in-memory database, but is it possible done in iPhone?

This is the document state that in 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2024-12-08 06:53:07

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

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

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

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

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

您可以将这两个步骤合二为一,如下所示:

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

Are you possibly missing a semicolon in your SQL command string and wrong ending '}' instead of ')' ?

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". You init it then you told the comment pointer to store another string when you used the assign statement.

You can do those two step in 1 like so:

NSString *comment = [[NSString alloc] initWithString:@"CREATE TABLE Temp(Name text, Address text}";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文