开发者

Save result from SQL query to ArrayList Android

开发者 https://www.devze.com 2023-04-02 00:19 出处:网络
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

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


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 
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号