I have a database (sqlite on Android) with several columns:
_id, datum, hour, note
The database is created by this:
private static final String DATABASE_CREATE =
"create table worked (_id integer primary key autoincrement, "
+ "datum text not null, hour text not null, "
+ "note text);";
At the end I want the total of hours and minutes out of my database and return it to my mainActivity.
I tried to do this:
public int getTotalHours(){
Cursor c = db.rawQuery("select hour from " + DATABASE_TABLE, null);
int total = 0;
Date hours = new Date();
SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
c.moveToFirst();
for(int i = 0; i<c.getCount(); i++){
String uur = c.getString(i);
try {
hours = hourFormat.parse(uur);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total += hours.getHours();
}
return total;
}
But I’m already getting a error in my query. I also dont know how if the data is being extracted at the right way, I cant test that because of the query error…
The error I’m getting:
Failed to read row 0, column 1 from a cursorWindow which has 5 rows, 1 columns.
I hope I was in the right way, and that somebody can help me.
Thanks in advance!
Properly loop through your records, retrieve the proper column index, and close your cursor: