Browse SQLite Database Data at Glance

Reading Time: 1 minute

Let’s make new button that will show all SQLite database data at glance in the toast.

New-button-for-showing-the-data

Then, add method in MySQLiteAdapter class:

SQLite-database-data-at-glance

Good habit in accessing the data in SQLite database is to find column indexes first, and then to use it.
Change readAllData from void to String, and return

public void String readAllData(){

SQLiteDatabase db = helper.getWritableDatabase();

String column[] = {MySQLiteHelper.UID,MySQLiteHelper.FIRST_ROW,MySQLiteHelper.SECOND_ROW};

Cursor cursor = db.query(MySQLiteHelper.TABLE_NAME,column,null,null,null,null,null);

StringBuffer stringBuffer = new StringBuffer();

while (cursor.moveToNext())

{

int index_id = cursor.getColumnIndex(MySQLiteHelper.UID);

int index_first_row = cursor.getColumnIndex(MySQLiteHelper.FIRST_ROW);

int index_second_row = cursor.getColumnIndex(MySQLiteHelper.SECOND_ROW);

String name = cursor.getString(index_first_row);

String addr = cursor.getString(index_second_row);

int cursor_id = cursor.getInt(index_id);

stringBuffer.append(cursor_id + ” ” + name + ” ” + addr + ” ” + “\n”);

}

return stringBuffer.toString();

}

Reading-the-data-SQLite-using-indexes

This is more or less all about browse SQLite database data at glance. Usage is as follows:
go to MainActivity.java and type following code:

public void showAllData(View view) {

String allData = helper.readAllData();

Message.message(this,allData);

}

Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

*