0

I am using this code for getting values from DB, it works fine for iPhone 4s and iPhone 5. But when it returns different values in iPhone 5s and onwards. I don't know why is this working like this.

  NSMutableArray *array = [[NSMutableArray alloc] init];

// Setup the database object
sqlite3 *database;
if(!databasePath)
{
    [self getDbPath:_dbName];
}
char const *dbPath = [databasePath UTF8String];

// Open the database from the users filessytem
if(sqlite3_open(dbPath, &database) == SQLITE_OK)
{// Setup the SQL Statement and compile it for faster access

    //SQLIte Statement
    NSString *sqlStatement_userInfo =[[NSString stringWithFormat:@"Select * from "]stringByAppendingString:tablename];

    sqlite3_stmt *compiledStatement;


    if(sqlite3_prepare_v2(database, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
    {

        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {
            CGRect screenRect = [[UIScreen mainScreen] bounds];
            CGFloat screenWidth = screenRect.size.width;
            CGFloat screenHeight = screenRect.size.height;
            if((screenWidth==320 && screenHeight==480) || (screenWidth==320 && screenHeight==568))
            {
                // Init the Data Dictionary
                NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];

                NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                // NSLog(@"_userName = %@",_userName);

                NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
                // NSLog(@"_emailID = %@",_emailID);

                NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                // NSLog(@"_contactNumber = %@",_contactNumber);

                NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

                NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",colId] forKey:@"id"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",authcode] forKey:@"authcode"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",title] forKey:@"title"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",subtitle] forKey:@"subtitle"];

                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",hours] forKey:@"hours"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",notificationtype] forKey:@"notificationtype"];


                [array addObject:_dataDictionary];
            }
            else
            {
                // Init the Data Dictionary
                NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];

                NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                // NSLog(@"_userName = %@",_userName);

                NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
                // NSLog(@"_emailID = %@",_emailID);

                NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                // NSLog(@"_contactNumber = %@",_contactNumber);

                NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

                NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",colId] forKey:@"id"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",authcode] forKey:@"authcode"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",title] forKey:@"title"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",subtitle] forKey:@"subtitle"];

                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",hours] forKey:@"hours"];
                [_dataDictionary setObject:[NSString stringWithFormat:@"%@",notificationtype] forKey:@"notificationtype"];

                // [_dataDictionary setObject:[NSString stringWithFormat:@"%@",hour] forKey:@"ZIPCode"];

                [array addObject:_dataDictionary];
            }

        }
    }
    else
    {
        NSLog(@"No Data Found");
    }

    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
}

sqlite3_close(database);

return array;

In above query following code is working fine for 4s and 5 but it returns different values on all other devices :

NSString *colId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
    // NSLog(@"_userName = %@",_userName);

    NSString *authcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
    // NSLog(@"_emailID = %@",_emailID);

    NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
    // NSLog(@"_contactNumber = %@",_contactNumber);

    NSString *subtitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

    NSString *hours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
    NSString *notificationtype = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

For example: NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; Here it will return title on iPhone 5 but on iPhone 5s it returns me authcode.

Muhammad Umair
  • 583
  • 11
  • 32

1 Answers1

1

Never use select * from ... in a query. You have no idea what columns and in what order they will be returned.

Write your query with an explicit list of field names:

select userName, contactNumber, notificationType, ... from ...

This will ensure your index references are consistent and you know exactly what you will get.

rmaddy
  • 314,917
  • 42
  • 532
  • 579