Search code examples
iphonesqlitefmdb

FMDB Result set contains null iphone


I am using FMDB for my application to store data in sqlite. Here is my code

from AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application 

{

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

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

}
NSLog(@"DB opened successfully");

// full code not shown here

}

I have following code in ViewController

-(IBAction)insertButtonClicked:(id)sender

{ NSLog(@"in insertButtonCLicked");

[db beginTransaction];

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

[db commit];

}

-(IBAction)displayButtonClicked:(id)sender

{

NSLog(@"in DisplayCLicked");

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

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

while( [rs next])
{

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

}

When i run this code i get rs as null as shown in following (this is o/p from console)

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

2011-03-07 07:30:13.341 InsertDataSample[3092:20b] in insertButtonCLicked

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

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

Plz help me friends. I am working on FMDB from many days but i am unable to use FMDB . Thanks in advance


Solution

  • Insert this just after your query and FMDB will probably tell you exactly what the problem is.

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

    Update:

    Running this:

    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];
    

    produced this output:

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