Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Problem summing floats from SQLite database for android app

0

55 views

I'm having a problem with summing floats properly from my SQLite database. I have created my table as follows where I have set the column 'amount' as FLOAT through the String sql4.

I have an EditText 'value' where the user enters a value, then it is entered into the database as through the ContentValues cv2.

All of this works fine, when I check the database externally the decimal value is recorded. However when I try to retrieve it through the mCursor1 the output is shown as 9.0 instead of 9.18 after I made two consecutive entries of 2.21 and 6.97…….. However when I output the data into an xml file and do the addition in excel I achieve the 9.18. This would leave me to believe it must be something due to the mCursor1 but I'm only guessing. Any ideas of where my problem lies?

String sql4 = "create table tracker (_id INTEGER PRIMARY KEY " +
        "AUTOINCREMENT, category_name TEXT NOT NULL, date DATE NOT NULL, place NUMERIC NOT NULL," +
        "amount FLOAT NOT NULL, day_id INTEGER NOT NULL, week_id INTEGER NOT NULL," +
        "month_id INTEGER NOT NULL, year_id INTEGER NOT NULL)";

ContentValues cv2 = new ContentValues();
cv2.put("amount", value.getText().toString());
mDb.insert("tracker", null, cv2);

float total1 = 0;
            mCursor1 = mDb.rawQuery(
                    "SELECT SUM(amount) FROM tracker", null);
                if(mCursor1.moveToFirst()) {
                    total1 = mCursor1.getInt(0);
                }
            mCursor1.close();

            String a = Float.toString((float)total1);           
            displayTotal.setText("Total €" + a);

asked June 24, 2011 6:49 am CDT
posted via StackOverflow

3 Answers

1
 

Try using mCursor1.getDouble() or mCursor1.getFloat() instead of mCursor1.getInt().

answered June 24, 2011 7:24 am CDT
1
 
total1 = mCursor1.getInt(0); 

Here's your problem ;)

answered June 24, 2011 7:24 am CDT
yep
0
Best answer
 

you are using

mCursor1.getInt(0);

but you should use

mCursor1.getFloat(0);

answered June 24, 2011 7:24 am CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Android sqlite problem
January 6, 2011
SQlite database-Android
January 28, 2011