2011-08-24 42 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