Android Sqlite使用where in

使用where in子句能夠高效地查詢出符合某個字段集合的內容,例子以下java

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<ForumRecommendInfo> 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.的錯誤app

相關文章
相關標籤/搜索