mardi 26 janvier 2016

android sqlite query "!=" multiple whereargs

I want to retrieve a rows from my database that have a specific value in their date column and a non-zero value in their total column.

The following code, which only retrieves rows based on date, works.

private String[] whereArgs = new String[]{
        "12/28/2011"
};

protected Cursor doQuery() {
        Cursor result =
                db
                        .getReadableDatabase()
                        .query(DatabaseHelper.TABLE,
                                new String[]{"ROWID AS _id",
                                        DatabaseHelper.TAG,
                                        DatabaseHelper.DATE,
                                        DatabaseHelper.EXPRESSION,
                                        DatabaseHelper.TOTAL},
                                DatabaseHelper.DATE + "=?",
                                whereArgs,
                                null, null, DatabaseHelper.DATE);

        result.getCount();

        return (result);
    }

This code attempts to return rows with a specific date and exclude rows with a value of zero using a "!=" operator.

It returns an empty cursor.

    private String[] whereArgs = new String[]{
        "12/28/2011",
        "0"
};

protected Cursor doQuery() {
        Cursor result =
                db
                        .getReadableDatabase()
                        .query(DatabaseHelper.TABLE,
                                new String[]{"ROWID AS _id",
                                        DatabaseHelper.TAG,
                                        DatabaseHelper.DATE,
                                        DatabaseHelper.EXPRESSION,
                                        DatabaseHelper.TOTAL},
                                DatabaseHelper.DATE + "=?"
                                        + " AND " +
                                DatabaseHelper.TOTAL + " != ?",
                                whereArgs,
                                null, null, DatabaseHelper.DATE);

        result.getCount();

        Log.v("CURSOR", DatabaseUtils.dumpCursorToString(result));

        return (result);
    }

And here is the logcat output of the dumpCursorToString method that proves it returns empty

01-26 15:35:40.920 22138-22153/com.example.ryan.caloriecalc1 V/CURSOR: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@4c46fc9
                                                                   <<<<<

Aucun commentaire:

Enregistrer un commentaire