mardi 27 octobre 2015

how to get sum of a row from a SQLite database?

I know to to get sum of column but when try for row getting an erorr

this is my SQLite code to retrieve the sum of a row

public int getSumOfAllAvgofexpensesofaprevday(String tablename,String date) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        try {
            String query = "SELECT shoprent, houserent, maintain, medical, government_applicable_fees, otherbill, " +
                    "(shoprent + houserent + maintain + medical + government_applicable_fees + otherbill) as 'totalbill' " +
                    "FROM "+tablename+" WHERE "+date+" = date('now','-1 days')";
            Cursor c = db.rawQuery(query, null);
            //Add in the movetofirst etc here? see SO
            c.moveToFirst();
            int i=c.getInt(5);
            return i;
        } finally {
            db.close();
        }
    }

Error code look like this

10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail: Index 0 requested, with a size of 0
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at com.example.jawa.pos.CustomersDbAdapter.getSumOfAllAvgofexpensesofaprevday(CustomersDbAdapter.java:427)
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at com.example.jawa.pos.DailyReport_CSV_file$1.run(DailyReport_CSV_file.java:85)
10-27 15:36:01.354 17677-17877/com.example.jawa.pos E/SendMail:     at java.lang.Thread.run(Thread.java:856)
10-27 15:36:01.374 17677-17677/com.example.jawa.pos E/ViewSystem: ViewRootImpl #2 Surface is not valid.

Aucun commentaire:

Enregistrer un commentaire