-1

I'm trying to understand how to save result from SQL queries in android to an arraylist.  Actually I find a few questions but none of them can answer why my code is wrong. So here is what I'm trying to do :

        String query = null;
        query = "SELECT * FROM users WHERE objectId = "+objectId+" AND serverName "+serverName;

        SQLiteDatabase db;

        // Insert results from query in database.
        ArrayList <String[]> result = new ArrayList<String[]>();
        ResultSet rs = db.execSQL(query);
        int columnCount = rs.getMetaData().getColumnCount();
        while(rs.next())
        {
            String[] row = new String[columnCount];
            for (int i=0; i <columnCount ; i++)
            {
               row[i] = rs.getString(i + 1);
            }
            result.add(row);
        }

And the error is saying that I cannot convert void to ResultSet. Any suggestions how to fix that or which is the right way to do this.

Thanks in advance

Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • Duplicate: http://stackoverflow.com/questions/1354006/how-can-i-create-a-list-array-with-the-cursor-data-in-android – NSjonas Aug 31 '11 at 11:47
  • `SQLiteDatabase db=null;` and then you call `db.execSQL(query);` That isn't going to work...post the full code. – Squonk Aug 31 '11 at 11:50
  • @NSjonas: Why is it a duplicate? – Squonk Aug 31 '11 at 11:52
  • @MisterSquonk this is the whole code. – Android-Droid Aug 31 '11 at 11:54
  • 1
    Maybe duplicate was the wrong word. But the question has already been answered. Query your database for a cursor, then take the cursor and step through it, adding each element to an ArrayList. Their might be a better way but not to my knowledge. – NSjonas Aug 31 '11 at 11:58
  • @Bombastic: If `db` is `null` then you can't call `db.execSQL(..)` without getting a `NullPointerException` thrown. – Squonk Aug 31 '11 at 12:00
  • I removed that in my code,but my main question is how to fix the problem with the error? – Android-Droid Aug 31 '11 at 12:01
  • @Bombastic: `SQLiteDatabase db;` still means that `db` will be `null` as you haven't instantiated it yet. You obviously haven't shown the full code. – Squonk Aug 31 '11 at 12:04
  • Actually the main idea here is that I'm just writing a method where I need to save the results from sql query in arraylist.I will create a different class where I will initialize the Database and it's just for testing now.I'm not going to use db as null.It will be something like CustomSQLDatabase db with some parameters maybe.But for now you just pretend that the database is initialized. – Android-Droid Aug 31 '11 at 12:10
  • 1
    If you want people to help you then don't waste their time with "pretend" code. – Squonk Aug 31 '11 at 12:17

1 Answers1

1

Once your database is open for reading use db.query or db.rawQuery to get a cursor that can be used to create your ArrayList

cursor = db.rawQuery(<query>, null);
            if(cursor.getCount() > 0)
            {
                cursor.moveToFirst();
                //add to list here 
            }
Avinash
  • 839
  • 1
  • 10
  • 17