Android Sqlite使用where in

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

使用where in子句可以高效地查询出符合某个字段集合的内容,例子如下

ContentResolver resolver = SyncService.this.getContentResolver();
        String[] tIds = new String[infos.size()];
        StringBuilder tSelection = new StringBuilder(ForumRecommendInfo.ID + " in (");
        for (int i = 0; i < infos.size(); i++) {
            tIds[i] = ((ForumRecommendInfo) infos.get(i)).getId();
            if (i != infos.size() - 1) {
                tSelection.append("?,");
            } else {
                tSelection.append("?");
            }
        }
        tSelection.append(")");
        Cursor tCursor = resolver.query(BbsProvider.sForumRecommendUri, null,
                tSelection.toString(), tIds, null);

        if (tCursor != null && tCursor.moveToFirst()) {
            do {
                String tId = tCursor.getString(tCursor.getColumnIndex(ForumRecommendInfo.ID));
                Iterator tIterator = infos.iterator();
                while (tIterator.hasNext()) {
                    if (tIterator.next().getId().equals(tId)) {
                        tIterator.remove();
                    }
                }
            } while (tCursor.moveToNext());
            tCursor.close();
        }

注意,占位符附近不能有引号,否则会报Cannot bind argument at index * because the index is out of range.的错误

转载于:https://my.oschina.net/u/2360415/blog/1490454

你可能感兴趣的:(Android Sqlite使用where in)