2017-05-27 97 views
0
-(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數據庫中的浮動值,除了前兩個字段。但是,當我想用​​select查詢我不會和顯示的記錄,以獲取所有這些記錄,並顯示如下錯誤:如何獲取存儲在sqlite數據庫中的float值?

*終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「* + [NSString的stringWithUTF8String:]:NULL CSTRING」

回答

0

sqlite3_column_text替換與sqlite3_column_double抓取在上面的代碼中的浮點值。

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

以上是您可以申請獲得浮動值的方式。

+0

eventhough我用sqlite3_column_double其示值誤差如雙不能點與他人。你能否正確寫下陳述 – subbu

+0

我更新我的答案,查看。 –

0

使用:

sqlite3_column_double(聲明,列)

代替:

sqlite3_column_text(聲明,列)

如果整數

sqlite3_column_int(聲明,列)

0

甚至tho唉,我用雙獲取浮點值的顯示錯誤 任何一個可以告訴我怎麼寫合適的線路,這些線路下方

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

here pRoundTime字符串對象 – subbu

相關問題