How to fetch the all data based on a particular date?
I am using SQLite to store my information. I am storing the date as string format. Now I want to fetch the data based on the date, there might be more than one data for a single date. I have checked relevant questions and tried in my way but can not find the solution. Though I was able to get info of a single data for a particular date.
My code for fetching data from the database:
public ArrayList<ExpenseModel> getSingleExpenseDetails(String date){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
String query = "select * from " + TABLE_SAVE_EXPENSE + " where "+ COLUMN_EXPENSE_DATE+ " = '" + date+ "'";
Cursor cursor=sqLiteDatabase.rawQuery(query, null);
ExpenseModel expenseModel=new ExpenseModel();
ArrayList<ExpenseModel> expenseModels = new ArrayList<>();
Log.v("Title : ",""+title);
if (cursor.moveToFirst()){
do {
expenseModel.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_TITLE)));
expenseModel.setDescription(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_DESCRIPTION)));
expenseModel.setAmount(cursor.getInt(cursor.getColumnIndex(COLUMN_EXPENSE_AMOUNT)));
expenseModel.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_DATE)));
expenseModel.setCurrency(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_CURRENCY)));
Log.v("Info : ",""+cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_TITLE)));
expenseModels.add(expenseModel)
}while (cursor.moveToNext());
}
cursor.close();
sqLiteDatabase.close();
return expenseModels;
}
ExpenseModel class:
package app.shakil.com.dailyexpense.Models;
public class ExpenseModel {
private int id;
private String title;
private String description;
private String date;
private int amount;
private String currency;
public ExpenseModel(){
}
public ExpenseModel(String title,String description,String date,int amount,String currency){
this.title=title;
this.description=description;
this.date=date;
this.amount=amount;
this.currency=currency;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}
Solution 1:
One obvious mistake that you do in your code is that you initialize expenseModel
before the loop and use it inside the loop for all the rows:
ExpenseModel expenseModel=new ExpenseModel();
Move that line inside the loop:
do {
ExpenseModel expenseModel=new ExpenseModel();
expenseModel.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_TITLE)));
expenseModel.setDescription(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_DESCRIPTION)));
expenseModel.setAmount(cursor.getInt(cursor.getColumnIndex(COLUMN_EXPENSE_AMOUNT)));
expenseModel.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_DATE)));
expenseModel.setCurrency(cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_CURRENCY)));
Log.v("Info : ",""+cursor.getString(cursor.getColumnIndex(COLUMN_EXPENSE_TITLE)));
expenseModels.add(expenseModel)
}while (cursor.moveToNext());