如何在 Android 应用程序中更高效地编写 SQL lite 查询?

问题描述 投票:0回答:2

我正在使用 Jetpack Compose 为 Android 编写一个小型应用程序。我的应用程序中有一个 SQL 数据库,其中有一个名为“findDay”的字段,其中包含一周中的每一天,周一到周日。

在我的 DAO 中,我为一周中的每一天调用一个单独的 SQL 查询。我确信这是低效的。

@Query("SELECT COUNT() FROM finds WHERE findDay=\"MONDAY\"")
     suspend fun getMondayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"TUESDAY\"")
    suspend fun getTuesdayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"WEDNESDAY\"")
    suspend fun getWednesdayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"THURSDAY\"")
    suspend fun getThursdayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"FRIDAY\"")
    suspend fun getFridayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"SATURDAY\"")
    suspend fun getSaturdayTotal():Float

    @Query("SELECT COUNT() FROM finds WHERE findDay=\"SUNDAY\"")
    suspend fun getSundayTotal():Float

这是我的存储库。

suspend fun getMondayTotal():Float {
       return findDao.getMondayTotal()

   }

    suspend fun getTuesdayTotal():Float {
        return findDao.getTuesdayTotal()

    }

    suspend fun getWednesdayTotal():Float {
        return findDao.getWednesdayTotal()

    }

    suspend fun getThursdayTotal():Float {
        return findDao.getThursdayTotal()

    }

    suspend fun getFridayTotal():Float {
        return findDao.getFridayTotal()

    }

    suspend fun getSaturdayTotal():Float {
        return findDao.getSaturdayTotal()

    }

    suspend fun getSundayTotal():Float {
        return findDao.getSundayTotal()

    }

这是我的视图模型。

suspend fun getMondayTotal(): Float {
        return repository.getMondayTotal()
    }

    suspend fun getTuesdayTotal(): Float {
        return repository.getTuesdayTotal()
    }

    suspend fun getWednesdayTotal(): Float {
        return repository.getWednesdayTotal()
    }

    suspend fun getThursdayTotal(): Float {
        return repository.getThursdayTotal()
    }

    suspend fun getFridayTotal(): Float {
        return repository.getFridayTotal()
    }

    suspend fun getSaturdayTotal(): Float {
        return repository.getSaturdayTotal()
    }

    suspend fun getSundayTotal(): Float {
        return repository.getSundayTotal()
    }

这是我的用户界面。

@Composable
fun FindsGraph(viewModel: MainViewModel) {

    var mondayval: Float by remember { mutableStateOf(0f) }
    var tuesdayval: Float by remember { mutableStateOf(0f) }
    var wednesdayval: Float by remember { mutableStateOf(0f) }
    var thursdayval: Float by remember { mutableStateOf(0f) }
    var fridayval: Float by remember { mutableStateOf(0f) }
    var saturdayval: Float by remember { mutableStateOf(0f) }
    var sundayval: Float by remember { mutableStateOf(0f) }

    val scope2 = rememberCoroutineScope()


    scope2.launch {
        mondayval = viewModel.getMondayTotal()
        tuesdayval = viewModel.getTuesdayTotal()
        wednesdayval = viewModel.getWednesdayTotal()
        thursdayval = viewModel.getThursdayTotal()
        fridayval = viewModel.getFridayTotal()
        saturdayval = viewModel.getSaturdayTotal()
        sundayval = viewModel.getSundayTotal()
    }


    val bardata = listOf(
        BarData(mondayval, "MONDAY", color = Color(0xFF00BCD4)),
        BarData(tuesdayval, "TUESDAY", color = Color(0xFFFFEB3B)),
        BarData(wednesdayval, "WEDNESDAY", color = Color(0xFFF44336)),
        BarData(thursdayval, "THURSDAY", color = Color(0xFF4CAF50)),
        BarData(fridayval, "FRIDAY", color = Color(0xFF673AB7)),
        BarData(saturdayval, "SATURDAY", color = Color(0xFFDF780B)),
        BarData(sundayval, "SUNDAY", color = Color(0xffed615d))
    )




            Column (modifier = Modifier.fillMaxHeight()){


                BarChart(
                    dataCollection = ChartDataCollection(bardata),
                    barSpacing = 10.dp,
                    axisConfig = AxisConfig(
                        showAxes = true,
                        axisColor = Color.Black,
                        axisStroke = 10f,
                        minLabelCount = 3,
                        showGridLabel = true,
                        showGridLines = false

                    ),
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(400.dp)
                )
            }


        }

所以,这个方法有效。它最终为我显示了一个很好的图表,但我也意识到这是一种真正的蛮力方法,我确信有一种更有效的方法来做到这一点。我真的很感谢任何帮助完善这一点。我是一名自学成才的 Android 程序员,如果我丑陋的代码冒犯了任何真正的程序员,我很抱歉。

android sqlite kotlin
2个回答
0
投票

首先,我们都经历过,只要您想了解所有重要的事情,就没有人会对您的代码生气。

第二,是的,实际上有一种更好的方法来完成你所做的事情,那就是通过将参数传递给你在

Dao
中保存所需日期的函数:

@Query("SELECT COUNT() FROM finds WHERE findDay= :dayOfWeek")
 suspend fun getDayOccurrences(dayOfWeek : String):Float

现在您必须将此参数从 UI 一直传递到此

Dao

存储库:

suspend fun getDayOccurrences(dayOfWeek : String):Float {
    return findDao.getDayOccurrences(dayOfWeek)
}

然后继续这样,您所需要做的就是从 UI 中度过正确的一天。

注意:我强烈建议您在

variable
中每天都有一个
List
或所有天都有
viewModel
,然后使用
viewmodel
收集
ViewModelScope
中出现的事件,因为当前的方式是这样做会导致每次
scope2
发生时都会触发
Recomposition


0
投票

您可以使用

GROUP BY
子句。这样的说法:

SELECT findDay, COUNT() FROM finds GROUP BY findDay;

将返回包含日期名称及其出现次数的行。要在空间中使用此功能,您必须使用

@RawQuery
。整个代码可以如下所示:

data class DayWithCount(
    val findDay: String,
    val count: Int,
)

@Dao
interface MyDao {
    @RawQuery
    suspend fun getDaysWithCount(query: SupportSQLiteQuery): List<DayWithCount>
}

class Repository {
    suspend fun getDaysWithCount(): List<DayWithCount> {
        val query = SimpleSQLiteQuery(
            "SELECT findDay, COUNT() AS count FROM finds GROUP BY findDay"
        )
        return dao.getDaysWithCount(query)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.