0

I want to convert the sqlite data into integer array.

public Integer[] getch() {

        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor = database.rawQuery("SELECT sum(sales) FROM sales group by outlet_code order by ordered_date", null);
        // String[] array = new String[crs.getCount()];
        int columnIndex = 3;
        Integer[] in = new Integer[cursor.getCount()];

        if (cursor.moveToFirst())
        {
            for (int i = 0; i < cursor.getCount(); i++)
            {
                in[i] = cursor.getInt(columnIndex);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return in;
    }

I need result in following format:

int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800, 0,0,0,0};

Integer[] income =  controller.getch();

I'm getting the error :

Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

2

1 Answer 1

1

Why are you using columnIndex = 3, your sql query will return only 1 column i.e Sum(sales), so you should set your columnIndex value to 0

Try this

public Integer[] getch() {

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor cursor = database.rawQuery("SELECT sum(sales) FROM sales group by outlet_code order by ordered_date", null);
    // String[] array = new String[crs.getCount()];
    int columnIndex = 0;
    Integer[] in = new Integer[cursor.getCount()];

    if (cursor.moveToFirst())
    {
        for (int i = 0; i < cursor.getCount(); i++)
        {
            in[i] = cursor.getInt(columnIndex);
            cursor.moveToNext();
        }
    }
    cursor.close();
    return in;
}

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

then please mark it correct/accept the answer by taping 'right mark' icon next to Answer. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.