I have a room database which contains the mediastore data with various ColumnInfo like Title, Dateadded, Composer, Album, artist etc
now the simple mediastore query was ,
cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns(),MediaStore.Audio.Media.IS_MUSIC " = 1", null, SortOrder);
Where SortOrder = MediaStore.Audio.Media.TITLE
etc
I want to use the same logic to query the Room database where ColumnInfo are
@PrimaryKey(autoGenerate = true)
public long _Id;
public long _SongId;
public String _title;
public String _album;
public long _albumId;
public String _artist;
public long _artistId;
public String _path;
public int _trackNumber;
public long _duration;
public String _year;
public String _composer;
public String _dateAdded;
I have looked at this
basically I want to Query each Column using the name of that Column and sort using ASC or DESC
how can we do that.
CodePudding user response:
You can use something like :-
@Query("SELECT * FROM mediaentity ORDER BY "
"CASE WHEN lower(:sortByColumn) = lower('_id') AND :sortDirection THEN _id END ASC,"
"CASE WHEN lower(:sortByColumn) = lower('_id') AND NOT :sortDirection THEN _id END DESC,"
"CASE WHEN lower(:sortByColumn) = lower('_SongId') AND :sortDirection THEN _SongId END ASC, "
"CASE WHEN lower(:sortByColumn) = lower('_SongId') AND NOT :sortDirection THEN _SongId END DESC,"
"CASE WHEN lower(:sortByColumn) = lower('_title') AND :sortDirection THEN _title END ASC, "
"CASE WHEN lower(:sortByColumn) = lower('_title') AND NOT :sortDirection THEN _title END DESC "
// and so on, noting the comma except on the last
";"
)
abstract List<MediaEntity> getSortedMediaList(String sortByColumn, boolean sortDirection);
- using the in-built SQLite
lower
function could be omitted for both sides of the expression.
Or simpler, but less safe, you could use Something like :-
@RawQuery
abstract List<MediaEntity> rawQuery(SupportSQLiteQuery qry);
public List<MediaEntity> getSortedMediaListV2(String sortByColumn, boolean sortDirection) {
String sortOrder = " ASC ";
if (!sortDirection) {
sortOrder = " DESC";
}
String query = "SELECT * FROM mediaentity ORDER BY " sortByColumn sortOrder;
return rawQuery(new SimpleSQLiteQuery(query));
}
- However, if you pass an incorrect column name then it would fail.
Perhaps consider the following working example that utilises the above and, at the end, shows what happens if an incorrect column name is used:-
db = TheDatabase.getInstance(this);
dao = db.getAllDao();
dao.deleteAll();
dao.insert(new MediaEntity(100,"Breathe","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",5,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(99,"Speak to Me","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",4,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(98,"On the Run","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",3,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(102,"Time","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",10,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(97,"The Great Gig in the Sky","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",9,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(101,"Money","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",8,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(103,"Us and Them","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",7,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(96,"Any Colour You Like","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",1,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(104,"Brain Damage","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",6,180,"1971","Pink Floyd","2021-10-10"));
dao.insert(new MediaEntity(95,"Eclipse","Dark Side of the Moon",100100,"Pink Floyd",100,"path to DSOTM",2,180,"1971","Pink Floyd","2021-10-10"));
Log.d("MEDIAINFO","EXTRACT 1 - ID ascending");
for(MediaEntity m: dao.getSortedMediaList("_Id",true)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 2 - ID descending");
for(MediaEntity m: dao.getSortedMediaList("_Id",false)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 3 - Title ascending");
for(MediaEntity m: dao.getSortedMediaList("_title",true)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 4 - Title descending");
for(MediaEntity m: dao.getSortedMediaList("_title",false)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 5 - SongId ascending");
for(MediaEntity m: dao.getSortedMediaList("_SongId",true)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 6 - SongId descending");
for(MediaEntity m: dao.getSortedMediaList("_SongId",false)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 7 - TrackNumber ascending");
for(MediaEntity m: dao.getSortedMediaListV2("_tracknumber",true)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
Log.d("MEDIAINFO","EXTRACT 8 - TrackNumber descending");
for(MediaEntity m: dao.getSortedMediaListV2("_tracknumber",false)) {
Log.d("MEDIAINFO","ID = " m._Id " SongId = " m._SongId " Title = " m._title " track# = " m._trackNumber " Album is " m._album);
}
dao.getSortedMediaList("not a known column", true);
dao.getSortedMediaListV2("not a known column", true);
The Output to the log including :-
2021-10-18 11:42:48.938 D/MEDIAINFO: EXTRACT 1 - ID ascending
2021-10-18 11:42:48.946 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.946 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.946 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.946 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.946 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.947 D/MEDIAINFO: EXTRACT 2 - ID descending
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.949 D/MEDIAINFO: EXTRACT 3 - Title ascending
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.955 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.956 D/MEDIAINFO: EXTRACT 4 - Title descending
2021-10-18 11:42:48.959 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.959 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.959 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.959 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.959 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.960 D/MEDIAINFO: EXTRACT 5 - SongId ascending
2021-10-18 11:42:48.965 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.965 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.965 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.966 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.966 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.966 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.966 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.966 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.967 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.967 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.967 D/MEDIAINFO: EXTRACT 6 - SongId descending
2021-10-18 11:42:48.971 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.972 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.973 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.973 D/MEDIAINFO: EXTRACT 7 - TrackNumber ascending
2021-10-18 11:42:48.977 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
2021-10-18 11:42:48.978 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.978 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.978 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.978 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.979 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.979 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.979 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.979 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.980 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.980 D/MEDIAINFO: EXTRACT 8 - TrackNumber descending
2021-10-18 11:42:48.984 D/MEDIAINFO: ID = 4 SongId = 102 Title = Time track# = 10 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 5 SongId = 97 Title = The Great Gig in the Sky track# = 9 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 6 SongId = 101 Title = Money track# = 8 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 7 SongId = 103 Title = Us and Them track# = 7 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 9 SongId = 104 Title = Brain Damage track# = 6 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 1 SongId = 100 Title = Breathe track# = 5 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 2 SongId = 99 Title = Speak to Me track# = 4 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 3 SongId = 98 Title = On the Run track# = 3 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 10 SongId = 95 Title = Eclipse track# = 2 Album is Dark Side of the Moon
2021-10-18 11:42:48.985 D/MEDIAINFO: ID = 8 SongId = 96 Title = Any Colour You Like track# = 1 Album is Dark Side of the Moon
- The last line line
dao.getSortedMediaListV2("not a known column", true);
fails due to the incorrect column name (the previous line doesn't fail but the result's order probably not be as expected due to the incorrect column name being passed). The exception (as excpected) being
:-
2021-10-18 11:42:48.989 E/SQLiteLog: (1) near "known": syntax error
2021-10-18 11:42:48.989 D/AndroidRuntime: Shutting down VM
2021-10-18 11:42:48.992 E/AndroidRuntime: FATAL EXCEPTION: main
Process: a.a.so69602045javaroomsortbycolumnsviacase, PID: 17538
java.lang.RuntimeException: Unable to start activity ComponentInfo{a.a.so69602045javaroomsortbycolumnsviacase/a.a.so69602045javaroomsortbycolumnsviacase.MainActivity}: android.database.sqlite.SQLiteException: near "known": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM mediaentity ORDER BY not a known column ASC
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)