【问题标题】:Get all unique values from SQLite column as string array从 SQLite 列中获取所有唯一值作为字符串数组
【发布时间】:2013-04-29 06:49:52
【问题描述】:

我尝试使用 SELECT DISTINCT sql 命令从数据库库中获取所有唯一值。 但是当我的活动加载时出现异常,我在 logcat 中有这个错误代码:

05-05 09:08:32.637: E/AndroidRuntime(1314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: SELECT * FROM exerciseTable WHERE SELECT DISTINCTexercise_typefromexerciseTable

我认为我没有正确编写命令,这是我的代码:

public String[] getAllExercies() {
        String selecet = "SELECT DISTINCT" + COLUMN_EXERCISE + "from" + TABLE_NAME;
        Cursor c = ourDatabase.query(TABLE_NAME, null, selecet, null, null, null, null);
        int dayExercise = c.getColumnIndex(COLUMN_EXERCISE);

        String[] list = new String[c.getCount()-1];
        int j = 0;
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            list[j] = c.getString(dayExercise);
            j++;
        }

        return list;
    }

【问题讨论】:

    标签: android cursor android-sqlite


    【解决方案1】:

    我认为您应该首先查看这些答案 herehere 以了解 .query() 函数的工作原理。 请注意,使用ourDatabase.query()函数时,参数如下:

    String Table Name: The name of the table to run the query against
    String [ ] columns: The projection of the query, i.e., the columns to retrieve
    String WHERE clause: where clause, if none then pass null
    String [ ] selection args: The parameters of the WHERE clause
    String Group by: A string specifying group by clause
    String Having: A string specifying HAVING clause
    String Order By by: A string Order By by clause
    

    所以你的第三个变量应该是一个 WHERE 子句,类似于:

    String[] args = { "first string" };
    Cursor c = ourDatabase.query("TABLE_NAME", null, "exercise_type=?", args, null, null, null);
    

    由于您不需要 WHERE 子句,出于您的目的,您可能希望使用 rawQuery() 方法。

    String selecet = "SELECT DISTINCT " + COLUMN_EXERCISE + " FROM " + TABLE_NAME;
    ourDatabase.rawQuery(selecet, null);
    

    更新 试试here 的答案。做这样的事情:

    Cursor c = ourDatabase.query(true, "exerciseTable", new String[] {"exercise_type"}, null, null, "exercise_type", null, null, null);
    int dayExercise = c.getColumnIndex(COLUMN_EXERCISE);
    //... continue with your further code
    

    希望这有助于其他请评论。

    【讨论】:

    • 谢谢!我的将 Cursor 转换为字符串数组的循环可以与 rawQuery 方法一起使用吗?
    • 是的。这应该。游标的概念是相同的,只是查询的方式对两者来说是不同的。如果你只有一个简单的 Sqllite 查询,可以直接使用 rawQuery() 方法运行。
    • *您无需更改循环中的任何内容。
    • 我试过了,但我仍然强制关闭。这是猫日志:' 05-05 10:17:43.507: E/AndroidRuntime(1850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compile: SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable
    • 从报错中可以看出你的查询是错误的:SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable。它应该类似于:SELECT DISTINCT exercise_type from exerciseTable。请参阅我的更新答案。
    【解决方案2】:

    问题: 你没有维护单词之间的space

    说明:

    假设,String COLUMN_EXERCISE = "exercise";

    String TABLE_NAME = "tbl_workout";

    那么 String selecet = "SELECT DISTINCT" + COLUMN_EXERCISE + "from" + TABLE_NAME;

    简单地说,SELECT DISTINCTexercisefromtbl_workout

    解决方案:

    String selecet = "SELECT DISTINCT " + COLUMN_EXERCISE + " from " + TABLE_NAME;

    编辑:

    请使用以下语法来触发 rawQuery

    Cursor c = ourDatabase.rawQuery(selecet,null);

    希望对你有帮助!

    【讨论】:

    • 我试过了。我仍然有力量接近。这是猫日志:' 05-05 10:17:43.507: E/AndroidRuntime(1850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compile: SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable
    【解决方案3】:

    你错过了查询中的所有空格,你应该用这个替换:

    String selecet = "SELECT DISTINCT " + COLUMN_EXERCISE + " FROM " + TABLE_NAME;
    

    【讨论】:

    • 我知道它在那里......谢谢你的回答:)
    • 我还是被逼近了。这是猫日志:' 05-05 10:17:43.507: E/AndroidRuntime(1850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compile: SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2018-01-18
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多