2

I am trying to work on some Android database/ sql stuff and have run into a snag. I am getting some errors and rather than pasting in an enormous chunk of code here, I thought I would narrow the question a bit. I was following the second response in this thread:

Creating tables in sqlite database on android

And noticed that they used the "INTEGER" type for defining the column.

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
            db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" +
                KEY_STUID + " TEXT PRIMARY KEY, " +
                KEY_SUB1 + " TEXT NOT NULL, " +
                KEY_SUB2 + " TEXT NOT NULL, " +
                KEY_SUB3 + " TEXT NOT NULL, " +
                KEY_MARKS1 + " **INTEGER** NOT NULL, " +
                KEY_MARKS2 + " **INTEGER** NOT NULL, " +
                KEY_MARKS3 + " **INTEGER** NOT NULL);"
        );
    }

My database will be using dollar amounts and percentages, meaning I will need decimal places. My question is, can you use the definition of DOUBLE here? Will it work? Is there a better option like FLOAT?

The only reason I don't just try this in code is that my pile of errors may mask the underlying issue and I might get a wrong answer.

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • There is no Double (or Float) in SQLite. Use REAL. http://www.sqlite.org/datatype3.html – Simon Mar 17 '14 at 08:22
  • For monetary values, consider using fixed point integers instead. http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – laalto Mar 17 '14 at 08:29
  • Fantastic! Thanks a million for the help guys! – PGMacDesign Mar 17 '14 at 08:43

2 Answers2

3

Consider using Real if you can.

These are the primitive types you can use when creating tables:

NULL. The value is a NULL value.

INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.

TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

BLOB. The value is a blob of data, stored exactly as it was input.

More information here

Udi Oshi
  • 6,787
  • 7
  • 47
  • 65
1

use REAL datatype instead of Double.You can also use Text datatype for storing any kind of value.

user3355820
  • 272
  • 1
  • 8