简体   繁体   English

如何使用 Android Room 数据库并使用 SQLite 的 Case Expression 进行查询

[英]how to use Android Room Database and query using SQLite's Case Expression

I have a room database which contains the mediastore data with various ColumnInfo like Title, Dateadded, Composer, Album, artist etc我有一个房间数据库,其中包含带有各种 ColumnInfo 的 mediastore 数据,如标题、添加日期、作曲家、专辑、艺术家等

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 Where SortOrder = MediaStore.Audio.Media.TITLE

I want to use the same logic to query the Room database where ColumnInfo are我想使用相同的逻辑来查询 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;

I have looked at this我看过这个

basically I want to Query each Column using the name of that Column and sort using ASC or DESC基本上我想使用该列的名称查询每一列并使用 ASC 或 DESC 进行排序

how can we do that.我们怎么能做到这一点。

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.对于表达式的两边,可以省略使用内置的 SQLite lower函数。

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);最后一行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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM