2016-05-14 56 views
-1

我已經編寫了一個方法,根據模塊搜索數據庫以查找匹配的講師。但是,當我運行這個時,它爲所有三個保存相同的名字!重寫SQLite語句SELECT WHERE - Android Eclipse

這裏是方法:

public List<tableModules> getStudentsLecturers(String mod1, String mod2, String mod3) { 
     List<tableModules> studentModuleLecturer = new ArrayList<tableModules>();  
     // Select All Query to find lecturers depending on modules  
     Log.d("Lecturers", mod1); 
     Log.d("Lecturers", mod2); 
     Log.d("Lecturers", mod3); 
     String selectQuery = "SELECT DISTINCT " + Module_Lecturer + " FROM " + Table2 + " WHERE " + Module_Name + " = \'" + mod1 + "\' OR \'" + mod2 + "\' OR \'" + mod3 + "\'"; 
     Log.d("1", "1"); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do {    
       tableModules moduleLecturers = new tableModules();    
       moduleLecturers.modulelecturer = cursor.getString(0);   
       studentModuleLecturer.add(moduleLecturers);   
      } while (cursor.moveToNext()); 
     } 
     // return lecture list 
     return studentModuleLecturer;  
    } 

,然後在這裏稱爲MainActivity.java

List<tableModules> lecturers = db.getStudentsLecturers(mod1, mod2, mod3); 
       if (!lecturers.isEmpty()) 
       {   
       for (tableModules session: lecturers) 
       { 
        Editor editor = preferences.edit(); 
        //Save lecturers to list 
        for (int i=0; i < lecturers.size(); i++) { 
         if (counter == 0) 
         {      
         lecturer1 = session.modulelecturer.toString(); 
         editor.putString("lec1", lecturer1); 
         Log.d("Lecs", "1"); 
         editor.commit(); 
         counter++; 
         } 
         if (counter == 1) 
         {     
         lecturer2 = session.modulelecturer.toString(); 
         editor.putString("lec2", lecturer2); 
         Log.d("Lecs", "2"); 
         editor.commit(); 
         counter++; 
         } 
         if (counter == 2) 
         { 
         lecturer3 = session.modulelecturer.toString(); 
         editor.putString("lec3", lecturer3); 
         Log.d("Lecs", "3"); 
         editor.commit(); 
         counter++; 
         } 
         else 
         {        
          Log.d("Lecs", "ERROR"); 
         } 
        } 

我得到我節省了同樣的「session.modulelecturer.toString( );」到每一個,但我不知道如何改變SQL方法來選擇所有三個獨立模塊的所有三個講師。

回答

2

嘗試

" WHERE " + Module_Name + " = '" + mod1 + "' OR " + Module_Name + " = '" + mod2 + "' OR " + Module_Name + " = '" + mod3 + "'"; 

在實踐更換

" WHERE " + Module_Name + " = \'" + mod1 + "\' OR \'" + mod2 + "\' OR \'" + mod3 + "\'"; 

,你不能說

WHERE ModuleName = 'This' OR 'That' 

但你能說

WHERE ModuleName = 'This' OR ModuleName = 'That' 

我知道這是一個拖動來重寫每個可能的值的列名稱,但它是如何在SQL中工作。


或者,你可以寫一個更緊湊的形式:

WHERE ModuleName IN ('This', 'That', '...') 

所以你查詢可以成爲:

" WHERE " + Module_Name + " IN ('" + mod1 + "', '" + mod2 + "', '" + mod3 + "')"; 
+0

出於某種原因,這仍然返回相同的名稱爲所有三個講師! :( – Uyyyfgfar

+0

然後你爲所有的3個變量傳遞相同的值,在你的查詢中有一個錯誤,然後跳到我的眼睛,我修好了它,現在又出現了另一個錯誤。 –