- The name, gender, and age of all the kids.
// Answer for a:
Cursor cursor = db.query("kids",
new String[ ]{"name", "gender", "age"},
null, null, null, null, null);
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
String gender = cursor.getString(1);
int age = cursor.getInt(2);
display += String.format("%s %s %d\n", name, gender, age);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The name and age of the girls.
// Answer for b:
Cursor cursor = db.query("kids",
new String[ ]{"name", "age"},
"gender = 'F'",
null, null, null, null);
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
int age = cursor.getInt(1);
display += String.format("%s %d\n", name, age);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- the name and age of the boys that are 10 years or older.
// Answer for c
Cursor cursor = db.query("kids",
new String[ ]{"name", "age"},
"gender = ? and age >= ?",
new String[ ]{"M", "10"}, null, null, null);
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
int age = cursor.getInt(1);
display += String.format("%s %d\n", name, age);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The names for all kids, sorted in ascending order by name.
// Answer for d:
Cursor cursor = db.query("kids",
new String[ ]{"name"},
null, null, null, null, "name");
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
display += String.format("%s\n", name);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The name and age for all girls, sorted in descending order by age.
// Answer for e:
Cursor cursor = db.query("kids",
new String[ ]{"name", "age"},
null, null, null, null, "age desc");
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
int age = cursor.getInt(1);
display += String.format("%s %d\n", name, age);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The name and age for all kids, sorted by gender; within each gender, sort by age.
// Answer for f:
Cursor cursor = db.query("kids",
new String[ ]{"name", "age"},
null, null, null, null, "gender, age");
String display = "";
cursor.moveToFirst( );
do {
String name = cursor.getString(0);
int age = cursor.getInt(1);
display += String.format("%s %d\n", name, age);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The average age for all kids.
// Answer for g:
Cursor cursor = db.query("kids",
new String[ ]{"avg(age)"},
null, null, null, null, null);
String display = "";
cursor.moveToFirst( );
do {
double average = cursor.getDouble(0);
display += String.format("%6.3f\n", average);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);
- The average age listed by gender separately. For example:
F 8.653
M 9.189
// Answer for h:
Cursor cursor = db.query("kids",
new String[ ]{"gender", "avg(age)"},
null, null, "gender", null, null);
String display = "";
cursor.moveToFirst( );
do {
String gender = cursor.getString(0);
double average = cursor.getDouble(1);
display += String.format("%s %6.3f\n", gender, average);
}
while(cursor.moveToNext( ));
txtOutput.setText(display);