2013-05-05 71 views
2

我嘗試使用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; 
    } 

回答

2

我覺得你應該先以本身結帳這些答案herehere e .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 

希望這有助於其他請評論。

+0

謝謝! 我的循環將Cursor轉換爲字符串數組可以很好的使用rawQuery方法嗎? – tomer 2013-05-05 10:16:44

+0

是的。這應該。光標的概念是一樣的,但查詢的方式對兩者都是不同的。如果你只是有一個簡單的Sqllite查詢,你可以直接使用rawQuery()方法來運行它。 – 2013-05-05 10:38:55

+0

*你不需要改變循環中的任何東西。 – 2013-05-05 10:45:11

0

你錯過所有的空間在您的查詢,你應該用這個代替:

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

我知道它在那裏... 謝謝你的回答:) – tomer 2013-05-05 10:14:50

+0

我還是強行關閉。 這裏是貓日誌:' 05-05 10:17:43.507:E/AndroidRuntime(1850):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.workoutlog/com.example.workoutlog。 AddWorkOutPage}:android.database.sqlite.SQLiteException:near「SELECT」:語法錯誤(代碼1):在編譯時:SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable – tomer 2013-05-05 10:19:55

1

Issue: 您還沒有保留這個詞之間的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);

我希望這會有所幫助!

+0

偉大的答案謝謝:) – tomer 2013-05-05 10:14:28

+0

我試過了。 我仍然有力量關閉。 這裏是貓日誌:' 05-05 10:17:43.507:E/AndroidRuntime(1850):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.workoutlog/com.example.workoutlog。 AddWorkOutPage}:android.database.sqlite.SQLiteException:near「SELECT」:語法錯誤(代碼1):編譯時:SELECT * FROM exerciseTable WHERE SELECT DISTINCT exercise_typefrom exerciseTable – tomer 2013-05-05 10:22:10

+0

@tomer:檢查我更新的答案 – 2013-05-05 10:57:56

相關問題