1

I have an SQL method here. I would like to return the data in a String[] array.

How do I do that exactly?

Thank you!

public String[] getLikedSongs() {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] LikeSong;
        Cursor cursor = db.rawQuery(" SELECT " + COL_4 + " FROM " + Table_Name + " WHERE " + COL_4 + " IS NOT NULL", null);
        while (cursor.moveToNext()) {
            String note = cursor.getString(0);
        }
        cursor.close();
        db.close();
        return LikeSong;
    }

1 Answer 1

1

You must define the array's length and this can be done only after the Cursor fetches all the rows. Then set its length to the nimber of rows of the Cursor.
Then inside the while loop set the items of the array:

public String[] getLikedSongs() {
    String[] LikeSong = null;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT " + COL_4 + " FROM " + Table_Name + " WHERE " + COL_4 + " IS NOT NULL", null);
    if (cursor.getCount() > 0) {
        LikeSong = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()) {
            LikeSong[i] = cursor.getString(0);
            i++;
        }
    }
    cursor.close();
    db.close();
    return LikeSong;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That solved it! I highly appreciate your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.