Search code examples
androidsqlitecursor

How to iterate and retrieve over all data stored in sqlite database


I am having problem while retrieving data from sqlite database what I need is to retrieve all data on console. But I am getting only one rows data on console Here is the code to insert and retrieve data from Sqlite. Please specify what I am missing or doing wrong. Thanks for any help.

 public long InsertContacts(Contacts contacts) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(KEY_IMAGE, DbUtility.getBytes(contacts.getBmp()));
            contentValues.put(KEY_BABY_NAME, contacts.getBaby_name());
            contentValues.put(KEY_GENDER, contacts.getBaby_gender());
            contentValues.put(KEY_SET_DATE, contacts.getDate());
            contentValues.put(KEY_SET_TIME, contacts.getTime());


            return db.insert(TABLE_NAME, null, contentValues);


        }

        public Contacts retriveContactsDetails() {

            SQLiteDatabase db = this.getReadableDatabase();
            String[] columns = new String[]{KEY_IMAGE, KEY_BABY_NAME, KEY_GENDER, KEY_SET_DATE, KEY_SET_TIME};
            Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
           cursor.moveToFirst();
                while (cursor.isAfterLast() == false) {

                    byte[] blob = cursor.getBlob(cursor.getColumnIndex(KEY_IMAGE));
                    String name = cursor.getString(cursor.getColumnIndex(KEY_BABY_NAME));
                    String gender = cursor.getString(cursor.getColumnIndex(KEY_GENDER));
                    String date = cursor.getString(cursor.getColumnIndex(KEY_SET_DATE));
                    String time = cursor.getString(cursor.getColumnIndex(KEY_SET_TIME));

                    Log.d(TAG, DbUtility.getImage(blob) + name + "-" + gender + "-" + date + "- " + time); // I need to get all date here that have been inserted but i am getting only first rows data every time i insert.
                    cursor.moveToNext();
                    return new Contacts(DbUtility.getImage(blob), name, gender, date, time);

                }

                cursor.close();
                return null;
            }

        }

Contacts.java

public class Contacts {


    private Bitmap bmp;
    private String baby_name;
    private String baby_gender;
    private String date;
    private String time;

    public Contacts(Bitmap b, String n, String g, String d, String t) {
        bmp = b;
        baby_name = n;
        baby_gender = g;
        date = d;
        time = t;
    }
    public Bitmap getBmp() {
        return bmp;
    }

    public String getBaby_name() {
        return baby_name;
    }

    public String getBaby_gender() {
        return baby_gender;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }
}

Solution

  • You should change your retriveContactsDetails() to this:

    public List<Contacts> retriveContactsDetails() {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = new String[]{KEY_IMAGE, KEY_BABY_NAME, KEY_GENDER, KEY_SET_DATE, KEY_SET_TIME};
    
        List<Contacts> contactsList = new ArrayList<>();
    
        Cursor cursor;
        try {
            cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
    
            while(cursor.moveToNext()) {
                byte[] blob = cursor.getBlob(cursor.getColumnIndex(KEY_IMAGE));
                String name = cursor.getString(cursor.getColumnIndex(KEY_BABY_NAME));
                String gender = cursor.getString(cursor.getColumnIndex(KEY_GENDER));
                String date = cursor.getString(cursor.getColumnIndex(KEY_SET_DATE));
                String time = cursor.getString(cursor.getColumnIndex(KEY_SET_TIME));
    
                contactsList.add(new Contacts(DbUtility.getImage(blob), name, gender, date, time));
                Log.d(TAG, DbUtility.getImage(blob) + name + "-" + gender + "-" + date + "- " + time);
            }
        } catch (Exception ex) {
            // Handle exception
        } finally {
            if(cursor != null) cursor.close();
        }
    
        return contactsList;
    }
    

    Also, your Contacts class should be named Contact as it contains only a single instance of your object.