0

我有一个包含 100 列或更多列的表,其值可能为“是”或“否”。

我想根据用户的请求(复选框值表单)进行选择,以提取所有值为“yes”的行。所以一个例子可能是:

- 用户选择 checkbox1 checkbox4 和 checkbox7 并且所有其他都没有被选中。

查询可能是:

select name from table where (col1 = yes or col4 = yes or col7 = yes) A​​ND (col2= no and col3 = no and all other columns are set to no)

如何在 android 查询中实现这一点?顺便说一下,我发现这个实现不是很优雅,有人可以建议我实现这个目标的其他实现吗?

4

1 回答 1

0

这是固定代码。它包含一些测试代码。

        int numColumns = 10;
        boolean[] answers = new boolean[numColumns];
        String[] colNames = new String[numColumns];
        for(int i = 0; i < colNames.length; i++) {
            answers[i] = Math.random() > 0.5;
            colNames[i] = String.format("Col%d", i);
        }
        //-----------LOGIC STARTS HERE----------------
        // use a loop
        StringBuilder yesColumns = new StringBuilder();
        StringBuilder noColumns = new StringBuilder();
        for(int i = 0; i < answers.length; i++) {
            if(answers[i]) {
                if(yesColumns.length() > 0)
                    yesColumns.append(" OR ");

                yesColumns.append(colNames[i]).append("='yes'");
            }
            else {
                if(noColumns.length() > 0)
                    noColumns.append(" AND ");
                noColumns.append(colNames[i]).append("='no'");
            }
        }

        String yesCondition = yesColumns.toString();
        String noCondition = noColumns.toString();
        String query = String.format("select name from table where (%s) AND (%s)",
                                     yesCondition,
                                     noCondition);
        System.out.println(query);
        // SQLiteDatabase db;
        // Cursor cursor = db.rawQuery(query, null);
于 2012-05-25T11:32:22.923 回答