// AnimalsDb Example // Source Code File: MainActivity.java package it372.ssmith.animaldb; import android.content.ContentValues; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); SQLiteOpenHelper dbh = new AnimalDbHelper(this); try { // Create database and table. SQLiteDatabase db = dbh.getWritableDatabase( ); db.execSQL("create table animals(name text, animal_type text, age integer)"); // Populate table ContentValues row1 = new ContentValues( ); row1.put("name", "Bently"); row1.put("animal_type", "Dog"); row1.put("age", 6); db.insert("animals", null, row1); ContentValues row2 = new ContentValues( ); row2.put("name", "Simba"); row2.put("animal_type", "Cat"); row2.put("age", 5); db.insert("animals", null, row2); ContentValues row3 = new ContentValues( ); row3.put("name", "Cottontail"); row3.put("animal_type", "Rabbit"); row3.put("age", 3); db.insert("animals", null, row3); // Create cursor to query the database. Cursor cursor = db.query("animals", new String[ ] {"name", "animal_type", "age"}, null, null, null, null, null); TextView txtOutput = findViewById(R.id.txt_output); cursor.moveToFirst( ); String output = ""; do { String name = cursor.getString(0); String animalType = cursor.getString(1); int age = cursor.getInt(2); output += String.format("%s %s %d\n", name, animalType, age); } while(cursor.moveToNext( )); txtOutput.setText(output); } catch(SQLException e) { Toast toast = Toast.makeText(this, "Database not created", Toast.LENGTH_LONG); toast.show( ); } } }