android sqlite其中value可以是任何值

时间:2016-09-21 13:21:13

标签: android sqlite

我有一个函数,它接受月份和年份参数并返回月份和年份匹配的DB中的所有值,现在我需要查询db,其中year匹配变量但月份可以是任何月份,我想使用相同的函数而不是创建一个新函数,所以我用什么值来调用这个函数。

当前通话看起来像这样

            ArrayList<PaymentHistory> allPayment = dataSource.findfiltered("Transactions", yearToGet, monthToGet);

然后功能看起来像这样

public ArrayList<PaymentHistory> findfiltered(String dbName, String year,  String month){
    //this needs to be done still
    String whereClause = DBOpenHelper.YEAR + " = ? AND "+ DBOpenHelper.STATUS + " = ? AND "  + DBOpenHelper.MONTH + " = ?";
    String[] whereArgs = new String[] {
            year,
            "paid",  //we only want successful transactions
            month
    };
    Cursor cursor = database.query(dbName, allColumns, whereClause, whereArgs, null, null, null);  //must sort this

    ArrayList<PaymentHistory> paymentHistories = getListDatas(cursor);
    cursor.close();
    return paymentHistories;
}

2 个答案:

答案 0 :(得分:1)

在您的方法中进行以下更改。

public ArrayList<PaymentHistory> findfiltered(String dbName, String year,  String month){
        //this needs to be done still
        String whereClause = DBOpenHelper.YEAR + " = ? AND "+ DBOpenHelper.STATUS + " = ? AND "  + DBOpenHelper.MONTH + " = ?";
        String[] whereArgs = new String[] {
                year,
                "paid", //we only want successful transactions
                month
        };
        if(month == null) {
                whereClause = DBOpenHelper.YEAR + " = ? AND "+ DBOpenHelper.STATUS + " = ?";
                String[] whereArgs = new String[] {
                        year,
                        "paid"                        
                };
        }


        Cursor cursor = database.query(dbName, allColumns, whereClause, whereArgs, null, null, null); //must sort this

        ArrayList<PaymentHistory> paymentHistories = getListDatas(cursor);
        cursor.close();
        return paymentHistories;
}

现在,无论月份如何,您都需要获得结果,只需在月份的位置传递空值。

ArrayList<PaymentHistory> allPayment = dataSource.findfiltered("Transactions", yearToGet, null);

答案 1 :(得分:1)

如果需要按月过滤,请致电

 ArrayList<PaymentHistory> allPayment = dataSource.findfiltered("Transactions", yearToGet, monthToGet);

如果不需要按月过滤,请致电

 ArrayList<PaymentHistory> allPayment = dataSource.findfiltered("Transactions", yearToGet, "");

方法:

public ArrayList<PaymentHistory> findfiltered(String dbName, String year,  String month){
    //this needs to be done still

    String whereClause = DBOpenHelper.YEAR + " = ? AND "+ DBOpenHelper.STATUS + " = ?";
    Cursor cursor = null;
    if(month != "")
    {
        whereClause +=" AND "  + DBOpenHelper.MONTH + " = ?";
        cursor = database.query(dbName, allColumns, whereClause, new String[] {year,"paid",month}, null, null, null);  //must sort this
    }
    else{
        cursor = database.query(dbName, allColumns, whereClause, new String[] {year,"paid"}, null, null, null);  //must sort this
    }

    ArrayList<PaymentHistory> paymentHistories = getListDatas(cursor);
    cursor.close();
    return paymentHistories;
}