如何获取存储在sqlite数据库中的浮点值?

时间:2017-05-27 06:25:23

标签: ios objective-c sqlite

-(NSMutableArray *)getStudentsInfo
{
    NSMutableArray *studentsList = [[NSMutableArray alloc]init];
    sqlite3_stmt* getStatement;

    const char* sql = "select * from Person";

    if (sqlite3_prepare_v2(database, sql, -1, &getStatement, NULL) != SQLITE_OK)
    {
        NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    }
    else{

        while (sqlite3_step(getStatement) == SQLITE_ROW)
        {
            Person* pObj = [[Person alloc]init];

            pObj.pNumber = sqlite3_column_int(getStatement,0);

            pObj.pName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 1)];


            NSLog(@"Student Name is %@",pObj.pName);

            pObj.pRoundTime = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 2)];


            NSLog(@"Student Class is %@",pObj.pRoundTime);

            pObj.pRoundCount = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 3)];


            NSLog(@"Student Section is %@",pObj.pRoundCount);

            pObj.pRestTime = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 4)];


            NSLog(@"Student Section is %@",pObj.pRestTime);

             pObj.pAlertTime = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 5)];



             NSLog(@"Student Section is %@",pObj.pAlertTime);

             pObj.pPreparationTime = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 6)];



            NSLog(@"Student Section is %@",pObj.pPreparationTime);






            [studentsList addObject:pObj];
        }
        printf("Successfully retrieved data from database");

    }
    sqlite3_finalize(getStatement);

    return studentsList;

}

我在这里遇到一个问题,我存储的所有这些字段都存储为sqlite数据库中的浮点值,前两个字段除外。但是,当我想使用选择查询获取所有这些记录时,我不会获取并显示记录,并显示以下错误:

  

*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * + [NSString stringWithUTF8String:]:NULL cString'

3 个答案:

答案 0 :(得分:1)

sqlite3_column_text替换为sqlite3_column_double以获取上述代码中的浮点值。

NSNumber *floatValue = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement, column)];

以上是您可以申请获得浮动值的方法。

答案 1 :(得分:1)

使用:

sqlite3_column_double(statement, column)

而不是:

sqlite3_column_text(statement, column)

如果是整数:

sqlite3_column_int(statement, column)

答案 2 :(得分:1)

即使我使用double来获取浮点值,也会显示错误 任何人都可以告诉我如何为这些下面的行写下正确的行

pObj.pRoundTime = [NSString stringWithUTF8String:(char*)sqlite3_column_text(getStatement, 2)];