North Border Software




Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

Getting Started in Android Development

Part 40 - Retrieving and Viewing the SQLite Database

In this tutorial we turn -our focus to retrieving the data from the database and displaying it. We need to add code to the ShowDatabaseActvity.java file.

MembersDatabase showMembers = new MembersDatabase(this);
showMembers.open();
String members = showMembers.getData();
showMembers.close();

As before we need to create an object of the MembersDatabase class. We have called this object showMembers. We then open and close the database and we wish to create a method called getData() between the open and close methods. As previously, you can instruct Android Studio to create this method framework in the MemebersDatabase class. We want to return a string value that will be used to set the text in the showDatabase TextView of the ShowDatabaseActivity. The code for the getData() method is shown below.

public String getData() {
   String [] columns = new String[] {KEY_ROWID, KEY_NAME, KEY_STATUS};
   Cursor c = memebersDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
   String databaseOutput = "";
   int iRow = c.getColumnIndex(KEY_ROWID);
   int iName = c.getColumnIndex(KEY_NAME);
   int iStatus = c.getColumnIndex(KEY_STATUS);
   for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
      databaseOutput += c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iStatus) + "\n";
   }
   return databaseOutput;
}

We first need to set up an array of string values that reference the column headings.

String [] columns = new String[] {KEY_ROWID, KEY_NAME, KEY_STATUS};

An object of the Curser class is then setup and the table name and string array are passed as parameters.

Cursor c = memebersDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

There are a lot of parameters here and these are explained below:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

A string variable is setup and set to be empty.

String databaseOutput = "";

This string will be returned and used to set the text in the TextView that shows the database. The next three line of code set an integer variable to each column number.

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iStatus = c.getColumnIndex(KEY_STATUS);

Finally, we set up a for loop to move the cursor from the start of the database to the end of the database and for each row we concatenate the string with the retrieved values. This string is finally returned to the ShowDatabaseActivity.

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
   databaseOutput += c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iStatus) + "\n";
}

The last task in the ShowDatabaseActivity.java file is to set the TextView text to the returned string.

This is a good time to test the functionality by adding a few records and displaying them. A screenshot of our example is below:

Android SQLite database screenshot

After this tutorial you MembersDatabase.java file should look similar to the one below:

Your SqliteActivity.java file should look similar to the one below:

Your ShowDatabaseActivity.java file should look similar to the one below:


Download Download tutorial set 2




© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training