SQLite返回值不正确

时间:2014-03-14 18:26:20

标签: android sqlite

以下是我从一个表格中获取教授,建筑物,房间,楼层,主题,时间细节的函数的实现。

public List<NewButton> getAllTimingButtons(String building_name,String floor_name,String room_name) {

       List<NewButton> button_list = new LinkedList<NewButton>();

       //build the query
       String query = "SELECT  * FROM " + TimingTable;

       //get reference to writable DB
       SQLiteDatabase db = this.getWritableDatabase();
       Cursor cursor = db.rawQuery(query, null);


       if (cursor.moveToFirst()) {
           do {

               NewButton button = new NewButton();


               button.setBuildingName(cursor.getString(1));
               button.setFloorName(cursor.getString(2));
               button.setRoomName(cursor.getString(3));
               button.setTime(cursor.getString(4));
               button.setProfName(cursor.getString(5));
               button.setSubjName(cursor.getString(6));

               if(button.getBuildingName().equals(building_name) 
                       && button.getFloorName().equals(floor_name)
                       && button.getRoomName().equals(room_name))
               {
                    button_list.add(button);
                    Log.d("get timings",button.getProfName()+" "+button.getSubjName());
               }
           } while (cursor.moveToNext());
       }
       db.close();
       return button_list;
   }

这是我的表

db.execSQL("CREATE TABLE "+TimingTable+" ("+TimingId+ " INTEGER PRIMARY KEY, "+
              BuildingName+" TEXT, "+FloorName+" TEXT, "+RoomName+ " TEXT, "+
             Time+" TEXT, "+ProfessorName+" TEXT, "+SubjectName+" TEXT )");

问题是我的返回列表仅包含最后添加的按钮详细信息两次。我的debu g日志显示该表正确地保存了值。我可以做一些事情,比如返回一个字符串列表,其中按顺序添加值并一次读取三个但我想知道错误是什么。

1 个答案:

答案 0 :(得分:1)

使用列索引从光标中获取每个索引的值。 像这样:

cursor.getString(cursor.getColumnIndex(TimingId));
...

问候