2016-05-14 51 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个变量传递相同的值,在你的查询中有一个错误,然后跳到我的眼睛,我修好了它,现在又出现了另一个错误。 –