Android SQLite “BETWEEN” not inclusive or returns all records

有些话、适合烂在心里 提交于 2019-12-10 14:21:50

问题


Very simple task it would seem, trying to get a given range of records/rows in my SQLite db in Android app 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 the above selectionArgs I'm getting just the two rows, row 1 and 10. If I change arg to anything else I'm getting the whole lot (entire set of records/rows) as if there was no condition. Been banging my head for a day why this works the way it does, perhaps there's some special thing to know about the use of "between" in SQLite ? Any comments/help much appreciated.


回答1:


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));
}



回答2:


Seems problem from your side I checked and it works fine,

Method

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

Implementation

        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();
        }

Output

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


来源:https://stackoverflow.com/questions/9079875/android-sqlite-between-not-inclusive-or-returns-all-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!