iOSAppDev
iOSAppDev

Reputation: 2783

SQLite always returns (null) for the TEXT value in iphone

I am trying to retrive data from my table. But it always returns (null). Here is my code

    -(NSString *)filePath
{

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir=[paths objectAtIndex:0];
    return [documentDir stringByAppendingPathComponent:@"database.sql"];

}

-(void)openDB
{
    //sqlite#_open
    if(sqlite3_open([[self filePath]UTF8String], &newDB)!=SQLITE_OK)
    {
        sqlite3_close(newDB);
        NSAssert(0,@"Database fialed to open");
    }
}

-(void)readAttendeesFormeetingId:(NSInteger)mId
{

    NSLog(@"readAttendeesFormeetingId==> %d",mId);

    sqlite3_stmt *stmtAgenda=nil; 

    attendeesArray = [[NSMutableArray alloc]init];
    SQLiteConnClass *objsqlite1=[[SQLiteConnClass alloc]init];
    newdb=[objsqlite1 openDB];

    if (stmtAgenda==nil) {

        const char *sql1 = "SELECT DISTINCT name FROM MeetingAttendees where meetingId= ? AND isPresent = 1";
        if (sqlite3_prepare_v2(newdb, sql1, -1, &stmtAgenda, NULL) != SQLITE_OK) {

            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(newdb));
        }

    }

    sqlite3_bind_int(stmtAgenda, 1, mId);

    while(sqlite3_step(stmtAgenda) == SQLITE_ROW) {

        const char *agendaTitle=(const char *)sqlite3_column_text(stmtAgenda,4);

        NSString *stragendaTitle=agendaTitle == NULL ? nil:[[NSString alloc] initWithUTF8String:agendaTitle];


        //NSString *stragendaTitle = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(stmtAgenda, 4)];

        NSLog(@"agendaTitle %@",stragendaTitle);

        [attendeesArray addObject:stragendaTitle];

        [stragendaTitle release];                       
    }

    // Reset the statement for future reuse.
    sqlite3_reset(stmtAgenda);
    sqlite3_close(newDB);
}

Here is my table structure

enter image description here

I have name values in my table. Any of the row doesn't contain null value. Still I am getting null result.

Upvotes: 1

Views: 1011

Answers (1)

BP.
BP.

Reputation: 10083

Your SELECT statement is returning a table that has only one column (the distinct name column from MeetingAttendees), but your sqlite3_column_text call is trying to read from column index 4. Try to change the 4 to 0 and see if it returns the data you need.

Upvotes: 4

Related Questions