Search code examples
androidsqlitelogcat

display each row separate in logcat


I am learning to work with android and sqlite but have some troubles with reading info from a database.

I retrieve the information from a db but it is all in one line in logcat

 public List<WedstrijdenGeschiedenis> getAllWedstrijden() {
        List<WedstrijdenGeschiedenis> wedstrijden = new LinkedList<WedstrijdenGeschiedenis>();

        // 1. build the query
        String query = "SELECT  * FROM " + TABLE_WEDSTRIJD;

        // 2. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        // 3. go over each row, build wedstrijd and add it to list
        WedstrijdenGeschiedenis wedstrijd = null;
        if (cursor.moveToFirst()) {
            do {
                wedstrijd = new WedstrijdenGeschiedenis();
                wedstrijd.setId(Integer.parseInt(cursor.getString(0)));
                wedstrijd.setThuis(cursor.getString(2));
                wedstrijd.setBezoeker(cursor.getString(3));
                wedstrijd.setDatum(cursor.getString(1));

                // Add wedstrijd to wedstrijden
                wedstrijden.add(wedstrijd);
            } while (cursor.moveToNext());
        }

        Log.d("Alle wedstrijden in DB:", wedstrijden.toString());

        // return wedstrijden
        return wedstrijden;
    }

what i see in logcat is one tag(Alle wedstrijden in DB) with the info:

12-12 09:22:22.192: D/Alle wedstrijden in DB:(21506): [Wedstrijd [id=3, datum=null, thuis=99, Bezoekers=47], Wedstrijd [id=4, datum=null, thuis=99, Bezoekers=47], Wedstrijd [id=5, datum=null, thuis=1, Bezoekers=2], Wedstrijd [id=6, datum=null, thuis=3, Bezoekers=4], Wedstrijd [id=7, datum=null, thuis=Learn Android App Development, Bezoekers=Wallace Jackson], Wedstrijd [id=8, datum=3, thuis=1, Bezoekers=2], Wedstrijd [id=9, datum=Wallace Jackson, thuis=datum, Bezoekers=Learn Android App Development], Wedstrijd [id=10, datum=Wallace Jackson, thuis=datum, Bezoekers=Learn Android App Development]]

but what i want to see is each row from the db as a separate tag like this:

Tag: Wedstrijd id 3

Text: datum,thuis,bezoekers

so how can i make a Log.d for each row?


Solution

  • So write log in your while cycle like this:

        if (cursor.moveToFirst()) {
                    do {
                        wedstrijd = new WedstrijdenGeschiedenis();
                        wedstrijd.setId(Integer.parseInt(cursor.getString(0)));
           Log.d("ID:", String.valueOf(Integer.parseInt(cursor.getString(0))));
                        wedstrijd.setThuis(cursor.getString(2));
     Log.d("Thuis:", cursor.getString(2));
                        wedstrijd.setBezoeker(cursor.getString(3));
                        wedstrijd.setDatum(cursor.getString(1));
    
                        // Add wedstrijd to wedstrijden
                        wedstrijden.add(wedstrijd);
                    } while (cursor.moveToNext());
                }