-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

10
  • Duplicate: stackoverflow.com/questions/1354006/… Commented Aug 31, 2011 at 11:47
  • SQLiteDatabase db=null; and then you call db.execSQL(query); That isn't going to work...post the full code. Commented Aug 31, 2011 at 11:50
  • @NSjonas: Why is it a duplicate? Commented Aug 31, 2011 at 11:52
  • 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. Commented Aug 31, 2011 at 11:58
  • 1
    If you want people to help you then don't waste their time with "pretend" code. Commented Aug 31, 2011 at 12:17

1 Answer 1

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 
            }
Sign up to request clarification or add additional context in comments.

Comments

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.