创建临时表并使用 Android Room 对其执行查询

And*_*oom 5 sqlite android android-room

我正在为我的数据库使用 android room:我正在寻找一种方法来执行一系列操作,如后续代码所示:

@Query( "CREATE TEMP TABLE IF NOT EXISTS t1 (idx INTEGER, rnd_pos INTEGER)")
fun shuffle_subquery1()

@Query("DELETE FROM t1;")
fun shuffle_subquery2()

@Query("INSERT INTO t1 SELECT song_id, RANDOM() FROM song_table WHERE playlist_id=:playlistID")
fun shuffle_subquery3(playlist_id: Int)

@Query("WITH " +
            "i_rnd(idx, rnd_pos) as (SELECT r1.idx, (SELECT COUNT(*) FROM t1 as r2 WHERE r2.rnd_pos<r1.rnd_pos OR " +
            "           (r2.rnd_pos=r1.rnd_pos and r2.idx<r1.idx)) as rnd_pos FROM t1 as r1) " +
            "UPDATE song_table SET rnd_pos=(SELECT rnd_pos FROM i_rnd WHERE song_table.song_id=i_rnd.idx) WHERE playlist_id=:playlistID"
)
fun shuffle_subquery4(playlist_id: Int)

@Transaction
fun shuffle(playlist_id: Int) {
    shuffle_subquery1()
    shuffle_subquery2()
    shuffle_subquery3(playlist_id)
    shuffle_subquery4(playlist_id)
}
Run Code Online (Sandbox Code Playgroud)

这给出了错误error: UNKNOWN query type is not supported yet. You can use:DELETE, INSERT, SELECT, UPDATE public abstract void shuffle_subquery1()

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: t1) public abstract void shuffle_subquery2()

@Transaction
@Query(
    "CREATE TEMP TABLE IF NOT EXISTS t1 (idx INTEGER, rnd_pos INTEGER);\n" +
            "DELETE FROM t1;\n" +
            "\n" +
            "INSERT INTO t1 SELECT song_id, RANDOM() FROM song_table WHERE playlist_id=:playlistID;\n" +
            "\n" +
            "WITH\n" +
            "i_rnd(idx, rnd_pos) as (SELECT r1.idx, (SELECT COUNT(*) FROM t1 as r2 WHERE r2.rnd_pos<r1.rnd_pos OR \n" +
            "           (r2.rnd_pos=r1.rnd_pos and r2.idx<r1.idx)) as rnd_pos FROM t1 as r1)\n" +
            "UPDATE song_table SET rnd_pos=(SELECT rnd_pos FROM i_rnd WHERE song_table.song_id=i_rnd.idx) WHERE playlist_id=:playlistID;"
)
fun shuffle(playlist_id: Int)
Run Code Online (Sandbox Code Playgroud)

这个版本报错:error: Must have exactly 1 query in the value of @Query or @DatabaseView public abstract void shuffle(int playlist_id);

使用 Android Room 执行此操作的最佳(且有效)方法是什么?