Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Cursor - Two floats not adding if they are equal?
    primarykey
    data
    text
    <p>This is a strange problem, and I hope it has a simple solution. I have a database with encrypted values. I have created a cursor that will go through each of the entries in a table, decrypt the value from the column I need, and add the value to a variable, "total". I want the sum of all of the values in the column. Here is the code:</p> <pre><code>while (c.moveToNext()) { strTotal = c.getString(c.getColumnIndexOrThrow(KEY_TOTAL)); strTotal = sc.decrypt(strTotal); total = Float.valueOf(strTotal) + total; } </code></pre> <p>Now, here's the strange part. Let's suppose I have two values in the database: 2 + 4. After each is decrypted, it will correctly add them: 6. Now, if the values are equal: 2 + 2, for instance, the method returns "2" instead of "4". This happens even if it is off by a decimal (2 + 2.01 = 4.01, but 2 + 2 still outputs 2 for example).</p> <p>Is there something I am missing here? Thanks!</p> <p>EDIT:</p> <p>I've changed the code around just to see if the decryption was the problem and it is still giving me the same result:</p> <pre><code>float total = 0; String strTotal = "10"; while (c.moveToNext()) { try { //strTotal = sc.decrypt(c.getString(c.getColumnIndex(KEY_TOTAL))); total = Float.valueOf(strTotal) + total; } catch (Exception e) { Log.e(TAG, "exception", e); } } </code></pre> <p>This code is returning "10", even though there are 3 entries in the database! It looks like if two rows in the database have the same value in the KEY_TOTAL field, it is returning less results. Here is the query:</p> <pre><code>Cursor c = mDb.query(true, DATABASE_TABLE, new String[] {KEY_TOTAL}, KEY_TYPE + "=" + t, null, null, null, null, null); </code></pre> <p>If I pull the db and open it with a sqlite browser, and SELECT all of the rows, I am getting 3 still, however. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload