1

In my application i am showing data from database in a table view.My requirement is that from database i have to retrieve the data which will fall in the current month.I Have written the query but it is coming as 0.Actually i have 1 entry in the database with today's date,so my query should return that data,but it is showing as 0.Please help me.Thanks in advance.

My query is as follows:

public String addgroupincome(String grp) throws SQLException
{   
    long sum=0; 
    Cursor cursor1 = db.rawQuery(
             "SELECT SUM("+(KEY_TOTAL)+") FROM incomexpense WHERE date= Strftime('%Y-%m','now') AND category='Income' AND groups='"+grp+"'",null);
     if(cursor1.moveToFirst())
     {
       sum = cursor1.getLong(0);
     }
     cursor1.close();   
     String housetotal=String.valueOf((long)sum);    
     return housetotal; 
}

I am getting that total and showing in atextview in table layout..

final  String houtotal=db.addgroupincome(group1);  
     housetotal.setText(houtotal);
prakash .k
  • 635
  • 2
  • 12
  • 24

3 Answers3

0

Most probably nothing wrong with the query but the way you pass the query result to ListView. Can you show how you do it? Perhaps I could help.

Or you could take a look here or here


public int getCount() {
    DBHelper dbHelper = DBHelper.getDBAdapterInstance(this);
    int count = 0;

    try {
        dbHelper.openDataBase();

        String query = "select count(1) from t_model where upper(brandName) = upper('"
                + selectedBrand + "') order by modelName ASC";

        Cursor cursor = dbHelper.selectRecordsCursor(query, null);

        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }

        cursor.close();
        cursor = null;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        dbHelper.close();
    }

    return count;
}

and for the TextView should be as simple as

TextView tvCount = (TextView) findViewById(R.id.tvCount);
tvCount.setText("Count : " + getCount);

If you are having trouble debugging your query. Try http://sqlitebrowser.sourceforge.net/ or http://www.sqliteexpert.com/

Community
  • 1
  • 1
stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63
0

Why don't you try by giving column names of your table in your query..might it work out for you..specify the columns which you want to retrive..

Rekha
  • 901
  • 1
  • 11
  • 20
0
if (cursor.moveToNext()) {
  cursor.moveToFirst();
  while (cursor.isAfterLast() == false) {
    Log.i(ID, cursor.getInt(0) + "");
    cursor.moveToNext();
  }
  cursor.close();
} else {
  cursor.close();
  return null;
}

Try This Method....

legoscia
  • 39,593
  • 22
  • 116
  • 167
J.K
  • 2,290
  • 1
  • 18
  • 29