Reputation:
I have a database that contains decimals. The column is set up as ".. decimal(5,2) not null. What I'm trying to do is to find the sum of the column and displaying it in a text field by using this code..
public int getTotal() {
Cursor cursor = sqliteDatabase2.rawQuery(
"SELECT SUM(thedata) FROM thetable", null);
if(cursor.moveToFirst()) {
return cursor.getInt(0);
}
return cursor.getInt(0);
}
However, when returned the value is a whole number. It should be something like 20.75 instead of just 20.
I'm not very familiar with SQLite, so please help.
Upvotes: 0
Views: 3289
Reputation: 1006869
On top of Xperimental's answer, you are calling getInt()
on the Cursor
, and you are returning and int from getTotal()
. Here, int
is shorthand for integer, what you refer to as a "whole number". If you want floating-point values, use getFloat()
and return a float
or Float
.
Upvotes: 2
Reputation: 401
SQLite doesn't know the datatype DECIMAL
, so it could be possible, that it defaults back to the INTEGER
datatype it knows. Try to use another datatype, which is recognized by SQLite. The datatypes SQLite knows can be found on the following page:
http://www.sqlite.org/datatype3.html
Upvotes: 2