0

I saved data on sqlite db with year and month, now want to retrieve data based on year and month but object is always null. Here is my code

  public List<AddIncomeModel> fetch(String year,String month) {
    database = this.getReadableDatabase();
   // Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
    Cursor cursor=database.rawQuery("SELECT * FROM " + TABLE_NAME+" WHERE "  +YEAR +" = "+year+" AND " + MONTH + " = "+month,null);

    List<AddIncomeModel> contacts = new ArrayList<AddIncomeModel>();
    AddIncomeModel contactModel;
    if (cursor.getCount() > 0) {
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            contactModel = new AddIncomeModel();
            contactModel.setId(cursor.getInt(0));
            contactModel.setIncome_source(cursor.getString(1));
            contactModel.setDescription(cursor.getString(2));
            contactModel.setAmount(cursor.getString(3));
            contacts.add(contactModel);
        }
    }
    cursor.close();
    database.close();
    return contacts;
}

Please tell me where I am going wrong. Thanks

1
  • Which object is null? Commented Jul 25, 2018 at 11:24

1 Answer 1

2

In SQLiteDatabase.query() the selection comes in two parts. The where clause (a String) and the whereArgs (an array of String).

To add more than one condition to the where clause you can use AND or OR, just like && or || in Java.

A question mark in the where clause is bound to one of the Strings in the whereArgs array.

Try Like This

cursor = db.query(TABLE_NAME, new String[] { ID, YEAR , MONTH},
YEAR + " LIKE ? AND " + MONTH+ " LIKE ?",                           
new String[] {"%" + dan + "%", "%" + vrijeme + "%"},
null, null, null, null);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.