Android SQLite "BETWEEN" does not include or return all records

A very simple task, it would seem, is an attempt to get the given range of records / rows in my SQLite db in an Android application with the following:

String[] projection = {"_id"};
String selection = "_id between ? and ?";
String[] selectionArgs = {"1", "10"};

queryBuilder.query(db, projection, selection, selectionArgs, null, null, null);

With Args above, I get only two lines, lines 1 and 10. If I change arg to something else, I get the whole batch (the whole set of records / lines), as if there were no conditions. I banged my head during the day, why does it work the way it happens, maybe there is some special thing to know about the use of "between" in SQLite? Any comments / help are greatly appreciated.

+5
source share
2 answers

You can use this code.

private SQLiteDatabase productDatabase;
   public Cursor get_result(){
   Cursor c;
   String sql = "SELECT * FROM " + tableName +"WHERE _id BETWEEN" + value1 +"AND" + value2;
   c = productDatabase.rawQuery(sql, null);
}

Cursor cursor=get_result();
    if(cursor.moveToNext()){
    Log.d("count of product id="+cursor.getString(0));
}
+1
source

, , ,

public Cursor GetBetween(String[] projection,String selection, String[] args){
        return db.query(TBL_NAME, projection, selection, args, null, null, null);
    }

        String[] projection = new String[]{"_id"};
        String[] selectionArgs = new String[]{"1","3"};
        String selection = "_id between ? and ?";
        Cursor cursorGetBetween = helper.GetBetween(projection,selection, selectionArgs);
        startManagingCursor(cursorGetBetween);
        cursorGetBetween.moveToFirst();
        while(!cursorGetBetween.isAfterLast()){
            Log.d("value of cursorGetBetween", cursorGetBetween.getString(0));
            cursorGetBetween.moveToNext();
        }

01-31 18:42:58.176: D/value of cursorGetBetween(647): 1
01-31 18:42:58.176: D/value of cursorGetBetween(647): 2
01-31 18:42:58.176: D/value of cursorGetBetween(647): 3
0

All Articles