我嘗試使用SELECT DISTINCT sql命令從數據庫庫中獲取所有唯一值。 但我得到的時候我的活動加載例外,我在logcat的這個錯誤代碼:從SQLite列獲取所有唯一值作爲字符串數組
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;
}
謝謝! 我的循環將Cursor轉換爲字符串數組可以很好的使用rawQuery方法嗎? – tomer 2013-05-05 10:16:44
是的。這應該。光標的概念是一樣的,但查詢的方式對兩者都是不同的。如果你只是有一個簡單的Sqllite查詢,你可以直接使用rawQuery()方法來運行它。 – 2013-05-05 10:38:55
*你不需要改變循環中的任何東西。 – 2013-05-05 10:45:11