Android SQLite "BETWEEN"不包含或返回所有记录



这似乎是一项非常简单的任务,尝试在Android应用程序中的SQLite数据库中获取给定范围的记录/行,如下所示:

String[] projection = {"_id"};
String selection = "_id between ? and ?";
String[] selectionArgs = {"1", "10"};
queryBuilder.query(db, projection, selection, selectionArgs, null, null, null);

通过上面的选择Args,我只得到两行,第1行和第10行。如果我把arg改成其他任何东西,我就会得到整个批次(整个记录/行集),就好像没有条件一样。我已经想了一天了,为什么它会这样工作,也许在SQLite中使用"between"有一些特别的事情需要知道?非常感谢任何意见/帮助。

您可以使用此代码。

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

从你的角度来看似乎有问题,我检查过了,它运行良好,

方法

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

最新更新