我一直试图从sqlite数据库中获取所有行。但是我从以下代码中得到了最后一行。
DBController dbController = new DBController(this);
SQLiteDatabase database = dbController.getWritableDatabase();
Cursor fila = database.rawQuery("select diaCaptura from LecturasBD", null);
ArrayList<String> lista = new ArrayList();
String name = "";
if(fila.moveToFirst()){
while(fila.isAfterLast() == false){
name = fila.getString(fila.getColumnIndex("diaCaptura"));
lista.add(name);
fila.moveToNext();
}
}
答案 0 :(得分:1)
在Cursor
中循环使用while
的{{1}}循环不那么令人困惑(且更清晰)的方法是使用moveToNext();
DBController dbController = new DBController(this);
SQLiteDatabase database = dbController.getWritableDatabase();
Cursor fila = database.rawQuery("select diaCaptura from LecturasBD", null);
ArrayList<String> lista = new ArrayList();
String name = "";
while(fila.moveToNext()){
name = fila.getString(fila.getColumnIndex("diaCaptura"));
lista.add(name);
}
fila.close(); // don't forget to close your cursor
答案 1 :(得分:0)
尝试“moveToNext”(http://developer.android.com/reference/android/database/Cursor.html),因为“isAfterLast”仅在光标位于最后时才为真(也见上面的链接)。