This is the third part of SQLite for Android series tutorials with examples. Here you can find the first part – SQLite for Android Part 2: Insert Data
In this example is explained hot to view insered data in to already created database and table.
NOTE: Dont forget to add ButterKnife binding in onCreate() in MainActivity:
1 |
ButterKnife.bind(this); |
After creating Person.db file and student_table and adding values in it, now we are going to view the inserted data.
Layout
Add a button to the activity_main.xml layout:
1 2 3 4 5 6 7 8 9 |
<Button android:id="@+id/viewAll" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/view_all" app:layout_constraintEnd_toEndOf="@id/addButton" app:layout_constraintStart_toStartOf="@id/addButton" app:layout_constraintTop_toBottomOf="@id/addButton" /> |
Then we add viewAll() method in our DataBaseHelper class:
1 2 3 4 5 6 |
public Cursor getAllData() { // Creating the DB instance SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); Cursor cursorResult = sqLiteDatabase.rawQuery("SELECT * FROM " + STUDENT_TABLE, null); return cursorResult; } |
This method returns Cursor. Cursor is interface that provides random read-write access to the result set returned * by a database query . Raw query is for querying from the data base. In this case we SELECT * (all) FROM table name and null for now, which means get all values from STUDENT_TABLE. This is for storing the data from database to the Cursor object so we can access this data in our MainActivity.
Alert Dialog for showing data
1 2 3 4 5 6 7 |
public void showDialogWithDbData(String title, String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(true); builder.setTitle(title); builder.setMessage(message); builder.show(); } |
In MainActivity, we are creating Dalog. In this Alert Dialog we set title and a message for showing the results.
Get all data implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@OnClick(R.id.viewAll) public void onViewAllClick() { Cursor data = dataBase.getAllData(); if (data.getCount() == 0) { showDialogWithDbData("Error", "No data found"); } else { // String appending StringBuilder stringBuilder = new StringBuilder(); while (data.moveToNext()) { stringBuilder.append("ID: ").append(data.getString(0)).append("\n"); stringBuilder.append("NAME: ").append(data.getString(1)).append("\n"); stringBuilder.append("LAST NAME: ").append(data.getString(2)).append("\n"); stringBuilder.append("DATE OF BIRTH: ").append(data.getString(3)).append("\n\n"); } // Show all data in Alert Dialog showDialogWithDbData("Data", stringBuilder.toString()); } } |
Cursor is for getting the data that is created in getAllData() method that is previously created. We have check if the data is empty. If is not, we apply adding records and values from the database to the Alert Dialog. The numbers in getString() methods are index of the columns in the database table. Index 0 is ID, Index 1 is NAME etc. The method append(“some string”) is used for adding some text to the string that we are appending to.
While is used for iterating trough out values in the table in the database. This is actually a very long string that is build with the StringBuilder class. It will add string to the stringBuilder as long as moveToNext() boolean method is true. After there is nothing to move to next, the iteration is stopped and the string is ready for showing in the Alert Dialog.