选择每一行作为android room库中的字符串列表

2uluyalo  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(368)

我在房间里有一个数据库表,使用此方案:

data class TestModel(
    val name: String,
    val phone: String,
    val description: String,
    val date: String,
    val profession: String,
    @PrimaryKey(autoGenerate = true)
    var id: Int? = null
)

我想选择每一行作为字符串列表。例如,对于包含此数据的行:

name = "testName"
    phone = "123456"
    description = "test"
    date: "2021/7/26"
    profession = "none"
    id: Int? = 1

我希望有以下输出:

arrayOf(arrayOf<String>("testName", "123456", "test", "2021/7/26", "none", "1"))

我怎样才能做到这一点?

q5lcpyga

q5lcpyga1#

我相信基础,即将数据提取为

"testName", "123456", "test", "2021/7/26", "none", "1"

可以是:-

@Query("SELECT name||','||phone||','||description||','||date||','||profession||','||id FROM testmodel")
abstract fun getColumnsAsCSV(): List<String>

如果你想要一个 ArrayList<List<String>> 由于每一行都是arraylist的一个元素,包含一个字符串列表,每一列1个,因此您可以(如果使用抽象类而不是@dao的接口)使用如下内容:-

@Dao
abstract class AllDao {

    @Insert
    abstract fun insert(testModel: TestModel): Long
    @Query("SELECT name||','||phone||','||description||','||date||','||profession||','||id FROM testmodel")
    abstract fun getColumnsAsCSV(): List<String>

    fun getColumnsAsListList(): ArrayList<List<String>> {
        var rv: ArrayList<List<String>> = ArrayList()
        val t = getColumnsAsCSV()
        for (s: String in t) {
            rv.add(s.split(","))
        }
        return rv
    }
}

然而,后者可以简单地从testmodel对象获得。或者只是拆分最初的摘录。
如果数据库包含:-

然后使用:-

/* Just extract the CSV */
    val t = dao.getColumnsAsCSV()
    for (s: String in t) {
        Log.d("TSTMDLINFO","String = $s")
    }
    /* extract as ArrayList<List<String>> */
    val tt = dao.getColumnsAsListList()
    var i = 0
    var ii = 0
    for(l: List<String> in tt) {
        i++
        ii = 0
        for (s: String in l) {
            ii++
            Log.d("TSTMDLINFO","Row # = $i String # = $ii String = $s")
        }
    }

结果:-

2021-07-26 19:49:25.297 D/TSTMDLINFO: String = testName,123456,test,2021/7/26,none,1
2021-07-26 19:49:25.297 D/TSTMDLINFO: String = testName,123456,test,2021/7/26,none,2
2021-07-26 19:49:25.297 D/TSTMDLINFO: String = testName,123456,test,2021/7/26,none,3

2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 1 String = testName
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 2 String = 123456
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 3 String = test
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 4 String = 2021/7/26
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 5 String = none
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 1 String # = 6 String = 1
2021-07-26 19:49:25.323 D/TSTMDLINFO: Row # = 2 String # = 1 String = testName
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 2 String # = 2 String = 123456
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 2 String # = 3 String = test
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 2 String # = 4 String = 2021/7/26
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 2 String # = 5 String = none
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 2 String # = 6 String = 2
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 3 String # = 1 String = testName
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 3 String # = 2 String = 123456
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 3 String # = 3 String = test
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 3 String # = 4 String = 2021/7/26
2021-07-26 19:49:25.324 D/TSTMDLINFO: Row # = 3 String # = 5 String = none
2021-07-26 19:49:25.325 D/TSTMDLINFO: Row # = 3 String # = 6 String = 3

相关问题