在Android的SQLite的选择NULL值Android、SQLite、NULL

2023-09-06 18:45:10 作者:奶油味拥抱

我执行下面的方法没有成功东阳的selectArgs的是不正确的(至少这是我相信。

I'm executing the following method with no success beacause of the selectArgs being incorrect (at least this is what I believe.

的findAll:

public Collection<Object> findAllByCodigoSetorOrderByStatusWhereDataAgendamentoIsNull(Integer vendedor) {
    Collection<Object> objects = null;
    String selection = Object.FIELDS[20] + "=?" + " OR " + Object.FIELDS[20] + "=?" + " OR " + Object.FIELDS[20] + "=?" + " AND " + Object.FIELDS[6] + "=?";
    String[] selectionArgs = new String[] { "''", "'null'", "NULL", String.valueOf(vendedor) };
    Collection<ContentValues> results = findAllObjects(Object.TABLE_NAME, selection, selectionArgs, Object.FIELDS, null, null, Object.FIELDS[4]);
    objects = new ArrayList<Object>();
    for (ContentValues result : results) {
        objects.add(new Object(result));
    }
    return objects;
}

findAllObjects:

protected Collection<ContentValues> findAllObjects(String table, String selection, String[] selectionArgs, String[] columns, String groupBy, String having, String orderBy) {
        Cursor cursor = null;
        ContentValues contentValue = null;
        Collection<ContentValues> contentValues = null;
        try {
            db = openRead(this.helper);
            if (db != null) {
                cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
                contentValues = new ArrayList<ContentValues>();
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToPosition(i);
                    contentValue = new ContentValues();
                    for (int c = 0; c < cursor.getColumnCount(); c++) {
                        contentValue.put(cursor.getColumnName(c), cursor.getString(c));
                    }
                    contentValues.add(contentValue);
                    cursor.moveToNext();
                }
            }
            return contentValues;
        } finally {
            close(db);
        }
    }

我怎样才能正确地选择和列比较 - 空,空,并使用db.query''

How can I correctly select and compare a column to - null, 'null' and '' using the db.query?

推荐答案

Android的数据库API不允许通过 NULL 值作为参数;它只允许琴弦。 (这是一个可怕的设计缺陷。更糟糕的是,SQLiteStatement 确实的允许所有类型的参数,但仅限于返回一个值的查询工作。)

Android's database API does not allow to pass NULL values as parameters; it allows only strings. (This is a horrible design bug. Even worse, SQLiteStatement does allow all types for parameters, but works only for queries that return a single value.)

您没有选择,而是要改变查询字符串等等IS NULL

You have no choice but to change the query string to blah IS NULL.