简体   繁体   English

问题汇总从SQLite数据库为Android应用程序浮动

[英]Problem summing floats from SQLite database for android app

I'm having a problem with summing floats properly from my SQLite database. 我在从SQLite数据库正确汇总浮点数时遇到问题。 I have created my table as follows where I have set the column 'amount' as FLOAT through the String sql4. 我创建了如下表,其中通过字符串sql4将列“金额”设置为FLOAT。

I have an EditText 'value' where the user enters a value, then it is entered into the database as through the ContentValues cv2. 我有一个EditText ',用户在其中输入一个值,然后通过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. 但是,当我尝试通过mCursor1检索它时,在我连续输入两个连续的2.21和6.97之后,输出显示为9.0,而不是9.18……。但是,当我将数据输出到xml文件并在excel中进行添加时达到9.18。 This would leave me to believe it must be something due to the mCursor1 but I'm only guessing. 这会让我相信这一定是由于mCursor1引起的,但我只是在猜测。 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);

尝试使用mCursor1.getDouble()mCursor1.getFloat()代替mCursor1.getInt()

you are using 您正在使用

mCursor1.getInt(0);

but you should use 但是你应该使用

mCursor1.getFloat(0);
total1 = mCursor1.getInt(0); 

这是你的问题;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM