How to get data from SQLITE database to array in android?

Pretty sure it’s easy, but I get confused by all the examples that adapt the data returned by the cursor into different views. I just want to run rawquery and put each data item returned in a float array (so that I can add them later). What do I need to use for this? Thanks

+3
source share
2 answers

You will still have a cursor when querying your database, but as soon as you get the cursor, you can iterate over it by pulling the values ​​you need into an array, for example:

DbAdapter db = new DbAdapter(mContext);
    int columnIndex = 3; // Whichever column your float is in.
    db.open();
    Cursor cursor = db.getAllMyFloats();
    float[] myFloats = new float[cursor.getCount()-1];

    if (cursor.moveToFirst())
    {                       
        for (int i = 0; i < cursor.getCount(); i++)
        {
            myFloats[i] = cursor.getFloat(columnIndex);
            cursor.moveToNext();
        }           
    }
    cursor.close();
    db.close();

    // Do what you want with myFloats[].
+9
source

1 float[] myFloats = new float[cursor.getCount()-1];, (int = 0) 0. , Java.lang.IndexOutOfBoundsException. [cursor.getCount()], [cursor.getCount()-1]. , float[] myFloats = new float[cursor.getCount()];

+3

Source: https://habr.com/ru/post/1788790/


All Articles