0

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?

santobedi
  • 866
  • 3
  • 17
  • 39
  • issue with inner join. Try these links https://stackoverflow.com/questions/6671678/objective-c-sqlite-join-tables-from-multiple-database https://stackoverflow.com/questions/17614974/how-to-join-two-tables-data-into-uitableview-using-sqlite-iphone-and-how-to-set – Gagan_iOS Sep 04 '17 at 09:11
  • try to print your SqlQuery and run directly in DB(use DB browser for sqlite) – Yatendra Sep 04 '17 at 09:20
  • @Gagan_iOS If there would be any issue with the inner join it wouldn't yield the total number of columns in the tables. I can see the total number of columns in both the tables (combined) displayed by NSLog. My code is not looping through the rows of the tables to fetch the data according to Row ID. – santobedi Sep 06 '17 at 02:13

0 Answers0