gpt4 book ai didi

android - sqlite查询安卓

转载 作者:行者123 更新时间:2023-11-29 00:34:13 25 4
gpt4 key购买 nike

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 时,该字符串不显示任何内容。

最佳答案

可能是因为你使用的条件

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

关于android - sqlite查询安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com