Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting initial column value in SQLite database to 0

Tags:

java

android

I am making an app where I have a certain checkbox called "Full tank". If i check this box i have to calculate a value mileage and save it into the database. If the box in not checked the mileage is not calculated and 0 is stored in the mileage column. However, When I make the first entry into my database I want the mileage to be 0 irrespective of the checkbox being checked or not.

Can anyone help me with the idea? Heres something I have tried uptil now, but this code generates the mileage for the first row as well:-

    public void calculateMileage(FuelExpense fe) 
    {

        // TODO Auto-generated method stub
        String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};

        long numRows = DatabaseUtils.queryNumEntries(ourDatabase, "fuel_table");
        if(fe.getTankFullstatus()==true)
        {
            mileageCursor = ourDatabase.rawQuery("SELECT * FROM fuel_table ORDER BY _id DESC LIMIT 1", null);

            if(mileageCursor!=null && mileageCursor.getCount()>0)
            {

                mileageCursor.moveToLast();

                String lastKm = mileageCursor.getString(1);
                String lastFuelQty = mileageCursor.getString(2);

                lastDBFuelEntry.setKm(Long.parseLong(lastKm));
                lastDBFuelEntry.setFuelQty(Double.parseDouble(lastFuelQty));
            }
            else
            {
                mileage = 0.0d;
            }

        }
        else
        {

            for(mileageCursor.moveToLast();!mileageCursor.isBeforeFirst();mileageCursor.moveToPrevious())
            {
                nftQty = nftQty+lastDBFuelEntry.getFuelQty();

            }

        }
        fuelUsed = nftQty + fe.getFuelQty();
        mileage =(fe.getKm() - lastDBFuelEntry.getKm())/ fuelUsed;
        mileage= (double)(Math.round(mileage*100))/100;
        fe.setMileage(mileage);
        }

Table creation:-
//Fuel info table
            db.execSQL( "CREATE TABLE "+FUEL_DATABASE_TABLE +" (" +
                    KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_KM + " LONG NOT NULL, "+
                    KEY_FUEL_QTY + " INTEGER NOT NULL, "+
                    KEY_FUEL_PRICE + " DOUBLE NOT NULL, "+
                    KEY_TOTAL_COST + " DOUBLE NOT NULL, "+
                    KEY_MILEAGE + " DOUBLE DEFAULT 0, "+
                    KEY_DATE + " DATE NOT NULL, "+
                    KEY_TANK_FULL + " TEXT);"
                    );

Insert:-
{
        // TODO Auto-generated method stub


        ContentValues cv = new ContentValues();
        cv.put(KEY_KM, fe.getKm());
        cv.put(KEY_FUEL_QTY, fe.getFuelQty());
        cv.put(KEY_FUEL_PRICE,fe.getFuelPrice());
        cv.put(KEY_TOTAL_COST, fe.getTotalCost());
        cv.put(KEY_MILEAGE, fe.getMileage());
        cv.put(KEY_DATE, fe.getFuelFilledDate());
        cv.put(KEY_TANK_FULL, fe.getTankFullstatus());
        return ourDatabase.insert(FUEL_DATABASE_TABLE, null, cv);
    }
like image 850
Pooja Gaonkar Avatar asked Mar 16 '26 18:03

Pooja Gaonkar


1 Answers

If I understand you correctly,

If you have only one entry in your database, you want to not calculate, and just show a zero.

I think all you need to do is change this:

if(mileageCursor!=null && mileageCursor.getCount()>0

to this:

if(mileageCursor!=null && mileageCursor.getCount()>1

And then, if you only have one entry in your database, the resultant calculation will be 0. I would assume your code is freaking out because you only have one entry, yet its trying to call the moveToPrevious() on your cursor.

like image 126
HalR Avatar answered Mar 18 '26 08:03

HalR