Android 以字符串数组形式从SQLite列获取所有唯一值
我尝试使用SELECTDISTINCT sql命令从数据库coulmn获取所有唯一值。 但我的活动加载时出现异常,logcat中有以下错误代码:Android 以字符串数组形式从SQLite列获取所有唯一值,android,cursor,android-sqlite,Android,Cursor,Android Sqlite,我尝试使用SELECTDISTINCT sql命令从数据库coulmn获取所有唯一值。 但我的活动加载时出现异常,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}: and
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;
}
如果查询中缺少所有空格,应替换为:
String selecet = "SELECT DISTINCT " + COLUMN_EXERCISE + " FROM " + TABLE_NAME;
我认为您应该首先签出这些答案,然后查看.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);
更新
试着从中找到答案。这样做:
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
希望这对其他人有帮助,请发表评论 问题:
您没有在单词之间保留空格
解释:
假设,String COLUMN\u EXERCISE=“EXERCISE”代码>
和String TABLE_NAME=“tbl_锻炼”代码>
然后
String selecet=“SELECT DISTINCT”+从“+表名”中选择列+练习+代码>
简单地说,选择DISTINCTexercisefromtbl\u锻炼
解决方案:
String selecet=“SELECT DISTINCT”+从“+表名”中选择列+练习+代码>
编辑:
请使用以下语法来启动rawQuery
Cursor c=ourDatabase.rawQuery(selecet,null)代码>
我希望这会有帮助 我知道它就在那里。。。谢谢你的回答:)我还差一点。以下是cat日志:“05-05 10:17:43.507:E/AndroidRuntime(1850):java.lang.RuntimeException:无法启动活动组件信息{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}:android.database.sqlite.SQLiteException:靠近“选择”:语法错误(代码1):,编译时:从练习表中选择*,从练习表中选择不同的练习类型谢谢!我将光标转换为字符串数组的循环将与rawQuery方法配合使用?是的。应该这样。游标的概念是相同的,但只是查询的方式不同。如果您只有一个简单的Sqllite查询,您可以直接使用rawQuery()方法来运行它。*您不需要更改循环中的任何内容。我尝试了它,但仍然实现了强制关闭。以下是cat日志:“05-05 10:17:43.507:E/AndroidRuntime(1850):java.lang.RuntimeException:无法启动活动组件信息{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}:android.database.sqlite.SQLiteException:靠近“选择”:语法错误(代码1):,编译时:从exerciseTable中选择*从exerciseTable中选择不同的练习类型从exerciseTable您可以从错误中看到您正在进行的查询是错误的:SELECT*从exerciseTable中选择不同的练习类型从exerciseTable
。它应该是这样的:从exerciseTable中选择不同的练习类型
。看看我更新的答案,我试过了。我还是离原力很近。以下是cat日志:“05-05 10:17:43.507:E/AndroidRuntime(1850):java.lang.RuntimeException:无法启动活动组件信息{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}:android.database.sqlite.SQLiteException:靠近“选择”:语法错误(代码1):,编译时:从exerciseTable中选择*,其中从exerciseTable中选择不同的练习类型