sai android
sai android

Reputation: 159

How to get multiple rows in SQLite at a time in Android?

I want to get multiple column from SQLite database for example I have the data with ids 1, 2, 3, 4, 5. Now in a SQLite database I want to get 2, 5 id values in Android:

    String selectQuery = "SELECT  * FROM " + Table_STOCK + " 
          WHERE " + KEY_ID + " = '" + id + "'" ;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

From the query above I can only get one at a time with one id. I want to get multiple columns with multiple ids.

Upvotes: 1

Views: 3033

Answers (3)

Björn Kechel
Björn Kechel

Reputation: 8433

You can use the keyword IN for this:

String commaSeparatedIds = "1, 2, 3, 4, 5";

String selectQuery = "SELECT  * FROM " + Table_STOCK + " 
                  WHERE " + KEY_ID + " IN (" + commaSeparatedIds + ")" ;

And here is a small helper method to create a commaSeparatedString from a List of Ids without additional Libraries:

private String toCommaSeparatedString(List<Integer> list) {
    if (list.size() > 0) {
        StringBuilder nameBuilder = new StringBuilder();
        for (Integer item : list) {
            nameBuilder.append(item).append(", ");
        }
        nameBuilder.deleteCharAt(nameBuilder.length() - 1);
        nameBuilder.deleteCharAt(nameBuilder.length() - 1);
        return nameBuilder.toString();
    } else {
        return "";
    }
}

Even Simpler with the apache commons libraries as explained here: Java: join array of primitives with separator (But beware that these sometimes cause problems with Android <4.4)

Upvotes: 4

Rishabh Saxena
Rishabh Saxena

Reputation: 1795

You can just add ids in array list and call this method, rest this method will take care of everything, its a dynamic approach because here you can pass as much id you can. Just Edit the Object with your own data model.

public ArrayList<Object> getContent(ArrayList<String> list) {

        ArrayList<Object> objList = new ArrayList<>();
        if(list!=null && !list.isEmpty()){
            SQLiteDatabase db = getReadableDatabase();

            //Check for list items
            for(int i =0;i<list.size();i++) {

                Cursor cursor = db.query(Table_STOCK, null,
                        KEY_ID + " = ?",
                        new String[]{list.get(i)}, null, null, null, null);


                Object obj=null;

                if (cursor != null) {
                    cursor.moveToFirst();

                    // Set your object properties extracting from cursor
                    obj = new Object();
                    obj.setColumn1Property(cursor.getString(cursor.getColumnIndex(Table_STOCK.COLUMN_1)));
                    obj.setColumn2Property(cursor.getString(cursor.getColumnIndex(Table_STOCK.COLUMN_2)));
                }
                if (cursor != null) {
                    cursor.close();
                }
                //Add extracted object in your list.
            objList.add(obj);

            }
        }
        return objList;
        //Return rows list from your database. 
    } 

Upvotes: 0

Praveen Rawat
Praveen Rawat

Reputation: 324

Here is the code, you have to use to get multiple records

  selectQuery = "SELECT  * FROM " + Table_STOCK + " WHERE " + KEY_ID + "='" + id2+ "' AND " + KEY_ID + " = '" + id5";

There are multiple ways to fetch the data using condition.

Upvotes: 1

Related Questions