Code example for SQLiteQueryBuilder

Methods: appendWhere, query, setDistinct, setTables

0
        return null; 
    } 
 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
 
Log.v("query()", uri.toString() +" AND "+ sUriMatcher.match(uri));
 
        SQLiteDatabase mDb = dbHelper.getReadableDatabase();
        String limit = null;
 
        switch (sUriMatcher.match(uri)) {
            case NEW_BOOKS:
                Cursor c = mDb.rawQuery("SELECT _id, DISTINCT book FROM new_test", null);
                c.setNotificationUri(getContext().getContentResolver(), uri);
                return c;
            case NEW_BOOK:
                qb.setTables(NewTest.TABLE);
                if (selection == null) {
                    selection = NewTest.Columns.BOOK + " = '" + uri.getLastPathSegment() +"'";
                } 
                break; 
            case NEW_BOOK_CAP:
                qb.setTables(NewTest.TABLE);
                qb.appendWhere(NewTest.Columns.BOOK + " = '" + uri.getPathSegments().get(2) +"'");
                if (selection == null) {
                    selection = NewTest.Columns.CAP + " = " + uri.getLastPathSegment();
                } 
                break; 
            case NEW_BOOK_VERSE_RANDOM:
                qb.setTables(NewTest.TABLE);
                if (selection == null) {
                    selection = NewTest.Columns.VERSE + " = " + uri.getPathSegments().get(1);
                } 
                if(sortOrder == null) {
                    sortOrder = "RANDOM()";
                } 
                limit = "1";
                break; 
            case NEW_BOOK_CHAPTERS_UNIQUE: {
                qb.setTables(NewTest.TABLE);
                qb.setDistinct(true);
                projection = new String[] {NewTest.Columns.CAP};
                if (selection == null) {
                    selection = NewTest.Columns.BOOK + " = '" + uri.getPathSegments().get(2) +"'";
                } 
                break; 
            } 
            case OLD_BOOKS:
                Cursor c2 = mDb.rawQuery("SELECT _id, DISTINCT book FROM old_test", null);
                c2.setNotificationUri(getContext().getContentResolver(), uri);
                return c2;
            case OLD_BOOK:
                qb.setTables(OldTest.TABLE);
                if (selection == null) {
                    selection = OldTest.Columns.BOOK + " = '" + uri.getLastPathSegment() +"'";
                } 
                break; 
            case OLD_BOOK_CAP:
                qb.setTables(OldTest.TABLE);
                qb.appendWhere(OldTest.Columns.BOOK + " = '" + uri.getPathSegments().get(2) +"'");
                if (selection == null) {
                    selection = OldTest.Columns.CAP + " = " + uri.getLastPathSegment();
                } 
                break; 
            case OLD_BOOK_VERSE_RANDOM:
                qb.setTables(OldTest.TABLE);
                if (selection == null) {
                    selection = OldTest.Columns.VERSE + " = " + uri.getPathSegments().get(1);
                } 
                if(sortOrder == null) {
                    sortOrder = "RANDOM()";
                } 
                limit = "1";
                break; 
            case OLD_BOOK_CHAPTERS_UNIQUE: {
                qb.setTables(OldTest.TABLE);
                qb.setDistinct(true);
                projection = new String[] {OldTest.Columns.CAP};
                if (selection == null) {
                    selection = OldTest.Columns.BOOK + " = '" + uri.getPathSegments().get(2) +"'";
                } 
                break; 
            } 
            default: 
                throw new IllegalArgumentException("Unknown query().URI: " + uri);
        } 
 
        Cursor c = qb.query(mDb, projection, selection, selectionArgs, null, null, sortOrder, limit);
        c.setNotificationUri(getContext().getContentResolver(), uri);
 
        return c;
    }