2011-08-24 40 views
0

sqlite能够创建内存数据库,但它可以在iPhone中完成吗?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; 
} 

回答

0

你可能丢失你的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}"; 
+0

非常感谢您!有用! – user909240