FMDB结果集包含null iphone

时间:2011-03-06 15:07:57

标签: iphone sqlite fmdb

我正在使用FMDB为我的应用程序在sqlite中存储数据。这是我的代码

来自AppDelegate的

- (void)applicationDidFinishLaunching:(UIApplication *)application 

{

FMDatabase * db = [FMDatabase databaseWithPath:@“/ tmp / sql.sqlite”];

if (![db open]) {
    NSLog(@"Could not open db.");

}
NSLog(@"DB opened successfully");

//此处未显示完整代码

}

我在ViewController中有以下代码

-(IBAction)insertButtonClicked:(id)sender

{     NSLog(@“in insertButtonCLicked”);

[db beginTransaction];

[db executeUpdate:@"insert into sample (url) values (?)",@"google'"];

[db commit];

}

- (IBAction为)displayButtonClicked:(ID)发送方

{

NSLog(@"in DisplayCLicked");

FMResultSet *rs = [db executeQuery:@"select url from sample"];

NSLog(@"Rs contains => %@",rs);

while( [rs next])
{

    NSLog(@"%@",[rs stringForColumn:@"url"]);
}
[rs close];

}

当我运行此代码时,我将rs作为null,如下所示(这是来自控制台的o / p)

2011-03-07 07:30:06.919 InsertDataSample[3092:20b] DB opened successfully

2011-03-07 07:30:13.341 InsertButtonCLicked中的InsertDataSample [3092:20b]

2011-03-07 07:30:16.860 DisplayCLicked中的InsertDataSample [3092:20b]

2011-03-07 07:30:16.860 InsertDataSample [3092:20b] Rs contains => (空)

Plz帮我朋友。我很多天都在研究FMDB,但我无法使用FMDB。 提前致谢

1 个答案:

答案 0 :(得分:3)

在查询后立即插入,FMDB可能会告诉您具体问题。

if ([db hadError]) {
    NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}

<强>更新

运行此:

FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/sql.sqlite"];
[db executeUpdate:@"create table sample (url TEXT);"];
[db executeUpdate:@"insert into sample (url) values(?);", @"google'"];
FMResultSet *rs = [db executeQuery:@"select url from sample;"];
while ([rs next]) {
    NSLog(@"%@", [rs stringForColumn:@"url"]);
}
[rs close];
[db close];

产生了这个输出:

2011-03-06 12:38:59.831 test[93163:207] google'