SQLite按月搜索

时间:2014-06-30 02:56:48

标签: android sqlite

我想根据一个月的时间搜索记录,但我现在没有成功,完整的日期还可以,但按月搜索却没有。这是我的帮手:

    public String getSingleTransacaoSum(String date) {

    SQLiteDatabase db = this.getReadableDatabase();
    double sum = 0;

    //Cursor cursorf = db.rawQuery("SELECT * FROM " + TABLE_TRANSACOES
        //  + " WHERE data = ? ", new String[] { date });

    Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_TRANSACOES+" WHERE strftime('%m', ' DATETIME') = ?",new String[] { date });

    while (cursor.moveToNext()) {
        sum += cursor.getDouble(cursor.getColumnIndex("valor"));
    }
    cursor.close();
    return String.valueOf(sum);
}

的活动:

    btGetSum.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                dia = (String) spinDia.getSelectedItem();
                mes = (String) spinMes.getSelectedItem();
                ano = (String) spinAno.getSelectedItem();
                String dataSendTo = dia + "/" + mes + "/" + ano;

                dbhelper.getSingleTransacaoSum(dataSendTo);

                tvValor.setText("R$ "
                        + dbhelper.getSingleTransacaoSum(dataSendTo));

            } catch (Exception erro) {
                mensagemExibir("Erro Ao Buscar", "" + erro.getMessage());
            }

        }
    });

如果有人可以提供帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

DATETIME是列的错误名称,它是函数的名称。 您必须删除撇号strftime(...)中的列名:

SELECT * FROM TABLE_TRANSACOES WHERE STRFTIME('%m', column_with_datetime) = ?

答案 1 :(得分:0)

这应该可以使用LIKE子句完成,IE如下

SELECT * FROM TABLE_TRANSACOES
WHERE DATETIME LIKE '%<month>%'

或在java中,

db.rawQuery("SELECT * FROM "+TABLE_TRANSACOES+" WHERE DATETIME LIKE '%/"+monthYouWantToSearchFor+"/%'");

假设DATETIME是您要查询的列。