问题
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