如何根据特定日期获取所有数据?

时间:2019-04-14 15:26:39

标签: android android-sqlite android-database android-date

我正在使用SQLite来存储我的信息。 我将日期存储为字符串格式。现在,我想根据日期获取数据,单个日期可能有多个数据。我已经检查了相关问题,并以我的方式尝试了一下,但找不到解决方案。虽然我可以获取特定日期的单个数据的信息。

我从数据库中获取数据的代码:

    def getPoints(self, x=0, y=0):
        n = self.layers/2
        res = []
        if self.left:
            if x<=0:
                res = res + self.left.getPoints(x-n, y-1)
            else:
                res = res + self.left.getPoints(x-n/2, y - 1)

        res.append((x, y, self.data))

        if self.right:
            if x >= 0:
                res = res + self.right.getPoints(x+n, y-1)
            else:
                res = res + self.right.getPoints(x+n/2 , y - 1)

        return res

ExpenseModel类:

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;
}

1 个答案:

答案 0 :(得分:0)

您在代码中犯的一个明显错误是在循环之前初始化expenseModel并将其在循环中用于所有行:

ExpenseModel expenseModel=new ExpenseModel();

在循环中移动该行:

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());