After setDist3(..) code stops by saying cursor has no data. What might be the problem! all the table and column name are matched. Why at the middle of the code it returns the else statement?
public List<Information> findTwoBus(Integer fromNode, Integer toNode){
List<Information> l_info = new ArrayList<Information>();
String SQL ="SELECT '2' AS type, s1.`bid` AS busid1, s3.`bid` AS busid2, '0' AS busid3, ch1.`changeid` AS changeid1, '0' AS changeid2,(s2.`distance` - s1.`distance`) AS dist1,(s4.`distance` - s3.`distance`) AS dist2, '0' AS dist3 FROM `changestations` AS ch1, `routes` AS b1, `routes` AS b2,`routestations` AS s1 INNER JOIN `routestations` AS s2 ON s1.`bid` = s2.`bid` INNER JOIN `routestations` AS s3 ON s2.`pid` = s3.`pid` INNER JOIN `routestations` AS s4 ON s3.`bid` = s4.`bid`WHERE s1.`pid` = :from AND s2.`pid` = ch1.`changeid` AND s4.`pid` = :to AND s2.`distance` > s1.`distance` AND s4.`distance` > s3.`distance` AND b1.`id` = s1.`bid` AND b2.`id` = s3.`bid` AND b1.`similarity` <> b2.`similarity` ORDER BY (dist1 + dist2) LIMIT 5";
Cursor res = myDataBase.rawQuery(SQL,new String[]{String.valueOf(fromNode), String.valueOf(toNode)});
if(res.getCount() > 0){
res.moveToFirst();
do{
Information info = new Information();
info.setType(Integer.parseInt(res.getString(res.getColumnIndex("type"))));
info.setBusid1(Integer.parseInt(res.getString(res.getColumnIndex("busid1"))));
info.setBusid2(Integer.parseInt(res.getString(res.getColumnIndex("busid2"))));
info.setBusid3(Integer.parseInt(res.getString(res.getColumnIndex("busid3"))));
info.setChangeid1(Integer.parseInt(res.getString(res.getColumnIndex("changeid1"))));
info.setChangeid2(Integer.parseInt(res.getString(res.getColumnIndex("changeid2"))));
//code runs upto here
info.setDist3(Integer.parseInt(res.getString(res.getColumnIndex("dist3"))));
// from here it says that cursor has no data in logcat and crashes
info.setDist2(Integer.parseInt(res.getString(res.getColumnIndex("dist2"))));
info.setDist1(Integer.parseInt(res.getString(res.getColumnIndex("dist1"))));
l_info.add(info);
res.moveToNext();
} while(!res.moveToLast());
} else{
Log.e("SQL Query Error","Cursor has no data");}
return l_info;
}
05-17 17:33:16.860 8665-8665/com.example.tripathee.gantabya E/SQL Query Error: Cursor has no data
05-17 17:33:16.860 8665-8665/com.example.tripathee.gantabya W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4167ec80)
答案 0 :(得分:0)
我相信你在while语句中遇到了问题:
while(!res.moveToLast());
不应该这样吗?:
while(!res.isAfterLast());