Code example for SQLiteQueryBuilder

Methods: appendWhere, query, setProjectionMap, setTables

0
        @Override 
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
 
            // Constructs a new query builder and sets its table name 
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(MainTable.TABLE_NAME);
 
            switch (mUriMatcher.match(uri)) {
                case MAIN:
                    // If the incoming URI is for main table. 
                    qb.setProjectionMap(mNotesProjectionMap);
                    break; 
 
                case MAIN_ID:
                    // The incoming URI is for a single row. 
                    qb.setProjectionMap(mNotesProjectionMap);
                    qb.appendWhere(MainTable._ID + "=?");
                    selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
                            new String[] { uri.getLastPathSegment() });
                    break; 
 
                default: 
                    throw new IllegalArgumentException("Unknown URI " + uri);
            } 
 
 
            if (TextUtils.isEmpty(sortOrder)) {
                sortOrder = MainTable.DEFAULT_SORT_ORDER;
            } 
 
            SQLiteDatabase db = mOpenHelper.getReadableDatabase();
 
            Cursor c = qb.query(db, projection, selection, selectionArgs,
                    null /* no group */, null /* no filter */, sortOrder);
 
            c.setNotificationUri(getContext().getContentResolver(), uri);
            return c;
        }