2
public String getCourse_info (String courseID) {

        String[] columns = new String[] {KEY_COURSE_ID, KEY_COURSE_TITLE, KEY_COURSE_CNUMBER, KEY_COURSE_SUBJECT, KEY_COURSE_DAYS,
                KEY_COURSE_START_TIME, KEY_COURSE_END_TIME, KEY_COURSE_PROFESSOR, KEY_COURSE_BUILDING, KEY_COURSE_ROOM_NUMBER };

        Cursor c = ourDataBase.query(DATABASE_TABLE_COURSES, columns, KEY_COURSE_ID + "=?", new String[]{courseID}, null, null, null);
        String result = "";

        int iCid = c.getColumnIndex(KEY_COURSE_ID);
        int iCtitle = c.getColumnIndex(KEY_COURSE_TITLE);
        int iCcnumber = c.getColumnIndex(KEY_COURSE_CNUMBER);
        int iCsubject = c.getColumnIndex(KEY_COURSE_SUBJECT);
        int iCdays = c.getColumnIndex(KEY_COURSE_DAYS);
        int iCstart_time = c.getColumnIndex(KEY_COURSE_START_TIME);
        int iCend_time = c.getColumnIndex(KEY_COURSE_END_TIME);
        int iCprofessor = c.getColumnIndex(KEY_COURSE_PROFESSOR);
        int iCbuilding = c.getColumnIndex(KEY_COURSE_BUILDING);
        int iCroom_number= c.getColumnIndex(KEY_COURSE_ROOM_NUMBER);

        for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
            result = result + c.getString(iCid)+ " " +c.getString(iCtitle)+ " "+c.getString(iCcnumber)+" "+ c.getString(iCsubject) + " "+
                    c.getString(iCdays)+ " "+ c.getString(iCstart_time)+ " "+ c.getString(iCend_time) +" " + 
                    c.getString(iCprofessor) + " "+ c.getString(iCbuilding) + " " + c.getString(iCroom_number)+ "\n" ;
        }
        Log.d("hello", result+"test2");
        return result;
    }

好的,我正在尝试在数据库中的一张表上运行此代码。问题是如果我通过数据库中的第一个 course_id,它就可以完美运行。但是当我尝试在之后放置 2nd 或任何 course_id 时,字符串不会显示任何内容。

4

2 回答 2

2

也许是因为你使用的条件

for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()) 

失败了。

表示光标在检索到第一条记录后到达记录的末尾。

使用这些语句并检查它是否在检索第一条记录后不会到达表的末尾。

if  (c.moveToFirst()) {

            Log.d("Entering","moveToFirst");
              do {
                        int iCid = c.getColumnIndex(KEY_COURSE_ID);
    int iCtitle = c.getColumnIndex(KEY_COURSE_TITLE);
    int iCcnumber = c.getColumnIndex(KEY_COURSE_CNUMBER);
    int iCsubject = c.getColumnIndex(KEY_COURSE_SUBJECT);
    int iCdays = c.getColumnIndex(KEY_COURSE_DAYS);
    int iCstart_time = c.getColumnIndex(KEY_COURSE_START_TIME);
    int iCend_time = c.getColumnIndex(KEY_COURSE_END_TIME);
    int iCprofessor = c.getColumnIndex(KEY_COURSE_PROFESSOR);
    int iCbuilding = c.getColumnIndex(KEY_COURSE_BUILDING);
    int iCroom_number= c.getColumnIndex(KEY_COURSE_ROOM_NUMBER);

result = result + c.getString(iCid)+ " " +c.getString(iCtitle)+ " "+c.getString(iCcnumber)+" "+    c.getString(iCsubject) + " "+
                c.getString(iCdays)+ " "+ c.getString(iCstart_time)+ " "+ c.getString(iCend_time) +" " + 
                c.getString(iCprofessor) + " "+ c.getString(iCbuilding) + " " + c.getString(iCroom_number)+ "\n" ;
    }

      }

       while (c.moveToNext());
        } 
        c.close();    
 }

如果它进入最后一条记录,那么它将产生ArrayIndexOutOfBoundsException

于 2012-11-28T11:05:50.337 回答
0

请参阅尝试使用此代码的 sqlite 查询,

Cursor query = db.query(table, columns, selection, selectionArgs, groupBy,
                having, orderBy);
if (query.getCount() > 0) {
        for (int i = 0; i < query.getCount(); i++) {
            int KEY_COURSE_ANY = query.getString(query.getColumnIndex(KEY_COURSE_ANY));
        }
    }
于 2012-11-28T11:52:43.410 回答