Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read floating numbers (REAL) from a sqlite database on the iphone?

I need to read floating point decimals from sqlite db

I have created Variable type in database as INTEGER

I am reading with sqlite3_column_int to NSInteger variables

which parts should changed so i can read floating point integers from database

thanks for answers sorry im a newbie in this topics

like image 662
bugra s Avatar asked Oct 08 '09 00:10

bugra s


People also ask

Does Iphone use SQLite?

Apple uses SQLite in many (most?) of the native applications running on Mac OS-X desktops and servers and on iOS devices such as iPhones and iPods. SQLite is also used in iTunes, even on non-Apple hardware.

What is real datatype in SQLite?

INTEGER – It is an integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the value. REAL – It is a floating point value, stored as an 8-byte floating number. TEXT – It is a string, stored using the database encoding (UTF).


3 Answers

There's no such thing as a floating-point integer. They're mutually exclusive on a fundamental level (well floating point numbers can fall on integer boundaries but you get the idea) :P.

But the way to get floats out is by using the sqlite3_column_double(sqlite3_stmt *, int) function.

NSNumber *f = [NSNumber numberWithFloat:(float)sqlite3_column_double(stmt, col)];
like image 145
Nick Bedford Avatar answered Oct 05 '22 11:10

Nick Bedford


Declare the fields as a floating point number. From sqllite datatype doc the database type is REAL. This is a 8 byte floating point number which is an Objective-C double, NSDouble or NSNumber type.

like image 40
mmmmmm Avatar answered Oct 05 '22 13:10

mmmmmm


NSNumber *num;
int temp = (float)sqlite3_column_double(walkstatement, 3);
num = [[NSNumber alloc]initWithInt:temp];
NSLog(@"%@",num);

This code worked for me..

like image 33
Ashutosh Avatar answered Oct 05 '22 12:10

Ashutosh