iOS:选择查询在某些设备上返回不同的结果

标签 ios objective-c sqlite

我使用此代码从数据库获取值,它在 iPhone 4s 和 iPhone 5 上运行良好。但是当它在 iPhone 5s 及更高版本中返回不同的值时。我不知道为什么会这样。

  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 上它会返回授权码。

最佳答案

永远不要在查询中使用select * from ...。您不知道将返回哪些列以及它们将以什么顺序返回。

使用明确的字段名称列表编写您的查询:

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

这将确保您的索引引用是一致的,并且您确切地知道您将得到什么。

关于iOS:选择查询在某些设备上返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246995/

相关文章:

ios - 在 isKindOfClass : 中未检测到从 UIButton 继承的自定义类

ios - 从第二个 View 返回到一个 View 时,UIButton 不可单击

ios - OpenCV 2.4.4 和 Tesseract 3.02.02 不要链接在一起

ios - iPhone 应用程序中的 Facebook 页面 "Like"

ios - Parse.com 是否可以使用 facebook 登录,同时检查 facebook 用户的电子邮件是否存在于解析中的用户中,以及是否链接两者?

java - 如何在objective-c中实现客户端Socket

iphone - 本地通知 : repeat "permanently" with arbitrary alert message and incremental badge

objective-c - sqlite 与 Objective C 的连接

sql - SQLite 中的有效表名是什么?

java - 我应该在哪里存储图像