我有一個房間資料庫,其中包含帶有各種 ColumnInfo 的 mediastore 資料,如標題、添加日期、作曲家、專輯、藝術家等
現在簡單的媒體存盤查詢是,
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 等等
我想使用相同的邏輯來查詢 ColumnInfo 所在的 Room 資料庫
@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;
我看過 這個
基本上我想使用該列的名稱查詢每一列并使用 ASC 或 DESC 進行排序
我們怎么能做到這一點。
uj5u.com熱心網友回復:
你可以使用類似的東西:-
@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);
lower對于運算式的兩邊,可以省略使用內置 SQLite函式。
或者更簡單,但不太安全,你可以使用類似的東西:-
@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));
}
- 但是,如果您傳遞了不正確的列名,那么它就會失敗。
也許考慮以下使用上述內容的作業示例,最后顯示如果使用不正確的列名會發生什么:-
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);
輸出到日志包括:-
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
dao.getSortedMediaListV2("not a known column", true);由于列名不正確,最后一行失敗(前一行沒有失敗,但由于傳遞了不正確的列名,結果的順序可能與預期不符)。例外(如預期的那樣)是
:-
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)
額外的
第三種方法是通過選擇合適的 Dao 的方法(可能在@Dao 類中,如下例所示)呼叫單個 Dao,例如:-
/* Method 3 */
@Query("SELECT * FROM mediaentity ORDER BY _id ASC")
abstract List<MediaEntity> getALlOrderBy_Id_ASC();
@Query("SELECT * FROM mediaentity ORDER BY _id DESC")
abstract List<MediaEntity> getALlOrderBy_Id_DESC();
@Query("SELECT * FROM mediaentity ORDER BY _SongId ASC")
abstract List<MediaEntity> getALlOrderBy_SongId_ASC();
@Query("SELECT * FROM mediaentity ORDER BY _SongId DESC")
abstract List<MediaEntity> getALlOrderBy_SongId_DESC();
@Query("SELECT * FROM mediaentity ORDER BY _title ASC")
abstract List<MediaEntity> getALlOrderBy_title_ASC();
@Query("SELECT * FROM mediaentity ORDER BY _title DESC")
abstract List<MediaEntity> getALlOrderBy_title_DESC();
@Query("SELECT * FROM mediaentity ORDER BY _trackNumber ASC")
abstract List<MediaEntity> getALlOrderBy_trackNumber_ASC();
@Query("SELECT * FROM mediaentity ORDER BY _trackNumber DESC")
abstract List<MediaEntity> getALlOrderBy_trackNumber_DESC();
/* and so on */
public List<MediaEntity> getSortedMediaListV3(String sortbyColumn, boolean sortDirection) {
List<MediaEntity> rv = new ArrayList(){};
if (sortDirection) {
switch ( sortbyColumn) {
case "_Id":
rv = getALlOrderBy_Id_ASC();
break;
case "_SongId":
rv = getALlOrderBy_SongId_ASC();
break;
case "_title":
rv = getALlOrderBy_title_ASC();
break;
case "_trackNumber":
return getALlOrderBy_trackNumber_ASC();
}
} else {
switch (sortbyColumn) {
case "_Id":
rv = getALlOrderBy_Id_DESC();
break;
case "_SongId":
rv = getALlOrderBy_SongId_DESC();
break;
case "_title":
rv = getALlOrderBy_title_DESC();
break;
case "_trackNumber":
rv = getALlOrderBy_trackNumber_DESC();
}
}
return rv;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325064.html
標籤:安卓 sqlite android-room
