views:

747

answers:

3

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

+2  A: 

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.

Mark
I second this. Declare the variable as a REAL instead of an INTEGER and use sqlite3_column_double to read the value as a double.
lostInTransit
thanks for pointing me sqllite datatype doc, i know it is somewhere around but i cant find that document
bugra s
+2  A: 

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)];
Nick Bedford
it works great thanks
bugra s
A: 

For additional information, sqlite3 uses a dynamic data type system. so anything past these:

  • NULL
  • INTEGER
  • REAL
  • TEXT
  • BLOB

are just hints to sqlite (technically, affinity), and it'll end up deciding on its own what type it is or is not.

http://www.sqlite.org/datatype3.html

the functions that return doubles, ints, etc are part of the sqlite C API try its best to give you what you want.... so what's stored in a column can potentially be converted by sqlite if what you're asking for is different from what's in the db.

http://www.sqlite.org/c3ref/column_blob.html

about midway down, there's a handy table that'll let you know what to expect.

pxl