从数据库中获取double值并使用它们进行计算

时间:2013-04-08 08:21:46

标签: java android sql database sqlite

我有另一个奇怪的问题,我只是不明白。

我正在使用max将双打保存到我的数据库中。小数点后3位数。 双打正确地存储在数据库中但是当我试图获取数据并添加/乘以存储的双打时,我得到一个奇怪的问题

如果我在小数点后只存储一个数字的数据,一切正常,我得到了正确的结果。 但如果我在小数点后添加2位或更多位数,我的整个计算将无法正常工作。我的TextViews没有结果。

我正在努力了解发生了几个小时的事情,但我不明白为什么会这样。

以下是代码:

My MainActivity,TextViews获取Text:

public void calc() {
        // Get text for the statistics
        amount_widget.setText(db.getSearchResult("amount", 0));     
        lpayment_widget.setText(db.getSearchResult("price", 0));
        tpayment_widget.setText(db.getSearchResult("total_price", 1));
        mileage_widget.setText(db.getSearchResult("mileage", 2));
        trefueled_widget.setText(db.getSearchResult("amount", 1));

        // Calculate text for the consumption
        double consumption = (Double.parseDouble(db
                .getSearchResult("amount", 1)) / Double.parseDouble(db
                .getSearchResult("mileage", 2))) * 100;
        consumption = Math.round(consumption * 100) / 100.0;
        consumption_widget.setText(consumption + "L /100km");
    }

我的数据库活动,其中提取和计算代码:

public String getSearchResult(String sql, int cmd) {

if (cmd == 0) {
    String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME
            + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
    Cursor cursor = db.rawQuery(countQuery, null);

    String tmp = cursor.moveToFirst() ? cursor.getString(0) : "0";

    cursor.close();

    return tmp;
} else if (cmd == 1) {
    double sum = 0;
    String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
    String idQuery = "SELECT _id FROM " + TABLE_NAME
            + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
    Cursor cursor = db.rawQuery(countQuery, null);
    Cursor id = db.rawQuery(idQuery, null);
    // berechnung
    cursor.moveToFirst();
    id.moveToFirst();

    int maxId = Integer.parseInt(id.getString(0));
    for (int i = 0; i < maxId; i++) {

        double tmp = Integer.parseInt(cursor.getString(0));
        sum = sum + tmp;
        cursor.moveToNext();
    }
    cursor.close();
    id.close();
    return String.valueOf(sum);
} else if (cmd == 2 && sql == "mileage") {
    double sum = 0;
    String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
    String idQuery = "SELECT _id FROM " + TABLE_NAME
            + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
    Cursor cursor = db.rawQuery(countQuery, null);
    Cursor id = db.rawQuery(idQuery, null);
    // berechnung
    cursor.moveToFirst();
    id.moveToFirst();

    int maxId = Integer.parseInt(id.getString(0));
    if (maxId > 1) {
        int array[] = new int[maxId];

        // Array füllen
        for (int i = 0; i < maxId; i++) {

            array[i] = Integer.parseInt(cursor.getString(0));
            // sum = sum + tmp;
            cursor.moveToNext();
        }
        for (int k = 1; k < maxId; k++) {
            double tmp;
            tmp = array[k] - array[k - 1];
            sum = sum + tmp;
        }

        cursor.close();
        id.close();
        return String.valueOf(sum);
    } else {
        return "--";
    }

}
return "Wrong CMD";
}

2 个答案:

答案 0 :(得分:0)

也许你犯了一个简单的错误。而不是:
consumption_widget.setText(consumption + "L /100km");
试试这个:

consumption_widget.setText(String.valueOf(consumption) + "L /100km");

答案 1 :(得分:0)

我发现了我的错误。

getSearchResult()方法中的我的DbAdapter类中,我有以下声明:

for (int i = 0; i < maxId; i++) {

        double tmp = Integer.parseInt(cursor.getString(0));
        sum = sum + tmp;
        cursor.moveToNext();
    }

这里我试图解析一个Integer但是把它写成一个double。这就是无法执行计算的原因。我需要像这样解析一个双:

for (int i = 0; i < maxId; i++) {

        double tmp = Double.parseDouble(cursor.getString(0));
        sum = sum + tmp;
        cursor.moveToNext();
    }

这是我的问题的解决方案