the_prole
the_prole

Reputation: 8945

How to get rows from multiple values of the same column

With this statment, I get get one row where column MediaStore.Audio.Albums._ID = 16:

    String[] stringArray = {"16"};
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
            new String[]{                                 // String[] projection (columns)
                    MediaStore.Audio.Albums.ALBUM_ART,
                    MediaStore.Audio.Albums.ARTIST,
                    MediaStore.Audio.Albums.ARTIST,
                    MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                    MediaStore.Audio.Albums.ALBUM_KEY
            },
            MediaStore.Audio.Albums._ID + "=?",           // String selection
            stringArray,                                  // String[] selectionArgs
            null,                                         // sortOrder
            null                                          // CancellationSignal
    );

Now how can I get multiple rows where the same column has multiple values e.g. MediaStore.Audio.Albums._ID = 16,25,24,30,...etc?

Something like this?

String[] stringArray = {"16","25","24","30","14","31","28","23","17","16","13"}; 
final Cursor mCursor = getContentResolver().query(
        MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
        new String[]{                                 // String[] projection (columns)
                MediaStore.Audio.Albums.ALBUM_ART,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                MediaStore.Audio.Albums.ALBUM_KEY
        },
        MediaStore.Audio.Albums._ID + "=?",           // String selection
        stringArray,                                  // String[] selectionArgs
        null,                                         // sortOrder
        null                                          // CancellationSignal
);

Upvotes: 0

Views: 410

Answers (2)

Fabio Venturi Pastor
Fabio Venturi Pastor

Reputation: 2529

Use the operator IN: http://www.w3schools.com/sql/sql_in.asp

final Cursor mCursor = getContentResolver().query(
        MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
        new String[]{                                 // String[] projection (columns)
                MediaStore.Audio.Albums.ALBUM_ART,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                MediaStore.Audio.Albums.ALBUM_KEY
        },
        MediaStore.Audio.Albums._ID + " IN(?,?)",           // String selection
         new String[]{"16","25"},                                  // String[] selectionArgs
        null,                                         // sortOrder
        null                                          // CancellationSignal
);

Hope this help you.

Upvotes: 0

JustMe
JustMe

Reputation: 710

Your mSelectionClause MediaStore.Audio.Albums._ID + "=?", is incorrect.

Documentation states that you need one "?" for each replaced argument.

Upvotes: 0

Related Questions