I have been using the following metod using a for loop but it occurred to me that I am only returning one row- so is it necessary to have a loop?
i.e my code resembles this:
public String topSwimmerSponsor() {
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result, " + KEY_NAME + " FROM "
+ DATABASE_TABLE + " ORDER BY result DESC limit 1", null);
String result = "";
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(1) + "\n";
}
return result;
}
However I am attempting to change it to the following:
public String topSwimmerSponsor() {
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result, " + KEY_NAME + " FROM "
+ DATABASE_TABLE + " ORDER BY result DESC limit 1", null);
String result = "";
result = result + c.getString(1) + "\n";
return result;
}
However my app crashes. What is the correct way to implement this? Thanks.
if you’re only expecting one result you can use this: