iOS sqlite: Looping through rows (sqlite3_step) with multiple tables to fetch the stored data

719 views Asked by At

In my iOS app I'm able to create a database with two tables and insert data into their respective rows/columns. I'm having a problem while fetching the data at this while(sqlite3_step(statement) == SQLITE_OK) loop.

The instance method for fetching the data is as follows:

-(NSArray)findData:(NSString*) rowID
{
    int rowNumber = 0;
    const char *dbpath = [databasePath UTF8String];
    if(sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:"@SELECT T1.ID, T1.X, T1.Y, T2.ID, T2.A, T2.B FROM T1 JOIN T2 ON T1.ID = T2.ID WHERE T1.ID = \"%@\"",rowID];
        const char* query_stmt = [querySQL UTF8String];
        NSMutableArray *returnArray = [[NSMutableArray alloc]init];
        if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL)==SQLITE_OK)
        {
            int columnCount = sqlite3_column_count(statement)
            NSLog (@"Total number of columns:%d", comumnCount);
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                rowNumber++;
                NSlog(@"Rownumber is: %d", rowNumber);
                for(int a=1; a<columnCount; a++)
                {
                    NSString *field = [[NSString alloc] initWithUTF8String:(cont char*) sqlite3_column_text(statement,a)];
                    [returnArray addObject:field];
                }
            }
            sqlite3_finalize(statement);
            sqlite3_close(database);
            return returnArray;
        }
    }
    return nil;
}

Here, the if condition sqlite3_prepare_v2 is good. It works fine and columnCount is printed. However, the while loop doesn't work. It works fine while fetching data from only one table. Is there anything to be added to the condition of multiple tables?

0

There are 0 answers