当数据传递给数据库处理程序时,有一个必须与SQLite匹配的值。但我仍然无法使用光标检索数据。
以下是检索数据的代码。
public String getBusInfoDest (String destin) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor1 = db.query(TABLE_BUS, new String[] { KEY_ROUTE , KEY_DESTINATION , KEY_FEE , KEY_TYPE , KEY_VIA1 , KEY_VIA2 , KEY_VIA3 }, KEY_DESTINATION + "=?", new String[] { destin }, null, null, null, null );
Cursor cursor2 = db.query(TABLE_BUS, new String[] { KEY_ROUTE , KEY_DESTINATION , KEY_FEE , KEY_TYPE , KEY_VIA1 , KEY_VIA2 , KEY_VIA3 }, KEY_VIA1 + "=?", new String[] { destin }, null, null, null, null );
Cursor cursor3 = db.query(TABLE_BUS, new String[] { KEY_ROUTE , KEY_DESTINATION , KEY_FEE , KEY_TYPE , KEY_VIA1 , KEY_VIA2 , KEY_VIA3 }, KEY_VIA2 + "=?", new String[] { destin }, null, null, null, null );
Cursor cursor4 = db.query(TABLE_BUS, new String[] { KEY_ROUTE , KEY_DESTINATION , KEY_FEE , KEY_TYPE , KEY_VIA1 , KEY_VIA2 , KEY_VIA3 }, KEY_VIA3 + "=?", new String[] { destin }, null, null, null, null );
if (cursor1 != null ) {
if (cursor1.moveToFirst()) {
String route = cursor1.getString(cursor1.getColumnIndex(KEY_ROUTE));
String destination = cursor1.getString(cursor1.getColumnIndex(KEY_DESTINATION));
String fee = cursor1.getString(cursor1.getColumnIndex(KEY_FEE));
String type = cursor1.getString(cursor1.getColumnIndex(KEY_TYPE));
String via1 = cursor1.getString(cursor1.getColumnIndex(KEY_VIA1));
String via2 = cursor1.getString(cursor1.getColumnIndex(KEY_VIA2));
String via3 = cursor1.getString(cursor1.getColumnIndex(KEY_VIA3));
return "Route: " + route + "\n" + "Destination: " + destination + "\n" + "Fare: " + fee + "\n" + "Type: " + type + "\n"
+ "Via: " + via1 + "\n" + " " + via2 + "\n" + " " + via3;
}
}
else if (cursor2 != null ) {
if (cursor2.moveToFirst()) {
String route = cursor2.getString(cursor2.getColumnIndex(KEY_ROUTE));
String destination = cursor2.getString(cursor2.getColumnIndex(KEY_DESTINATION));
String fee = cursor2.getString(cursor2.getColumnIndex(KEY_FEE));
String type = cursor2.getString(cursor2.getColumnIndex(KEY_TYPE));
String via1 = cursor2.getString(cursor2.getColumnIndex(KEY_VIA1));
String via2 = cursor2.getString(cursor2.getColumnIndex(KEY_VIA2));
String via3 = cursor2.getString(cursor2.getColumnIndex(KEY_VIA3));
return "Route: " + route + "\n" + "Destination: " + destination + "\n" + "Fare: " + fee + "\n" + "Type: " + type + "\n"
+ "Via: " + via1 + "\n" + " " + via2 + "\n" + " " + via3;
}
}
else if (cursor3 != null ) {
if (cursor3.moveToFirst()) {
String route = cursor3.getString(cursor3.getColumnIndex(KEY_ROUTE));
String destination = cursor3.getString(cursor3.getColumnIndex(KEY_DESTINATION));
String fee = cursor3.getString(cursor3.getColumnIndex(KEY_FEE));
String type = cursor3.getString(cursor3.getColumnIndex(KEY_TYPE));
String via1 = cursor3.getString(cursor3.getColumnIndex(KEY_VIA1));
String via2 = cursor3.getString(cursor3.getColumnIndex(KEY_VIA2));
String via3 = cursor3.getString(cursor3.getColumnIndex(KEY_VIA3));
return "Route: " + route + "\n" + "Destination: " + destination + "\n" + "Fare: " + fee + "\n" + "Type: " + type + "\n"
+ "Via: " + via1 + "\n" + " " + via2 + "\n" + " " + via3;
}
}
else if (cursor4 != null ) {
if (cursor4.moveToFirst()) {
String route = cursor4.getString(cursor4.getColumnIndex(KEY_ROUTE));
String destination = cursor4.getString(cursor4.getColumnIndex(KEY_DESTINATION));
String fee = cursor4.getString(cursor4.getColumnIndex(KEY_FEE));
String type = cursor4.getString(cursor4.getColumnIndex(KEY_TYPE));
String via1 = cursor4.getString(cursor4.getColumnIndex(KEY_VIA1));
String via2 = cursor4.getString(cursor4.getColumnIndex(KEY_VIA2));
String via3 = cursor4.getString(cursor4.getColumnIndex(KEY_VIA3));
return "Route: " + route + "\n" + "Destination: " + destination + "\n" + "Fare: " + fee + "\n" + "Type: " + type + "\n"
+ "Via: " + via1 + "\n" + " " + via2 + "\n" + " " + via3;
}
}
return "No data";
}
返回值也是“无数据”,只输入cursor1 if-else语句。
感谢。