2016-05-16 6 views
0

Я использую этот код для получения значений из БД, он отлично работает для iPhone 4 и iPhone 5. Но когда он возвращает разные значения в iPhone 5 и далее. Я не знаю, почему это работает так.iOS: выбор запроса возвращает разные результаты на некоторых устройствах

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; 

В приведенном выше запросе следующий код работает отлично для 4s и 5, но он возвращает различные значения на всех других устройствах:

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

Например: NSString *title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; Здесь будет возвращать название на iPhone 5 но на iPhone 5s он возвращает мне authcode.

+0

Мне нужно обнаружить это, потому что я использую базу данных в своем приложении, когда я использую select query для выбора данных из базы данных, он возвращает мне словарь. На iphone 4 и 5 индекс значений различен, а по 5s и другим другим индексам разные. Поэтому я хочу обнаружить устройство. –

+0

@MuhammadUmair Не используйте комментарий, обновите свой вопрос. –

+0

«На iphone 4 и 5 индекс значений отличается, а на 5s и всех других устройствах индекс отличается:« Как может возникнуть такая ситуация в первую очередь? – Gruntcakes

ответ

1

Никогда не используйте select * from ... в запросе. Вы не представляете, какие столбцы и в каком порядке они будут возвращены.

Написать свой запрос с явным списком имен полей:

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

Это обеспечит ваши ссылки индексов последовательны и вы точно знаете, что вы получите.

+0

Благодарим вас за ваше драгоценное время. Это значит многое. оставаться благословенным. У меня проблема. –

Смежные вопросы