1

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.

8
  • Please add more details in your question. And please search for answer before you are posting one. I think the problem you are facing is common. Commented Apr 18, 2018 at 6:34
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Apr 18, 2018 at 6:35
  • Please update question with more details. Commented Apr 18, 2018 at 6:37
  • 1
    this is very basics of SQL, you should learn before starting using it. You can use WHERE id in (2,5) instead of WHERE id = 2 Commented Apr 18, 2018 at 6:42
  • get multiple coloumn also you likely ment multiple rows Commented Apr 18, 2018 at 6:43

3 Answers 3

4

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

But for that you have to be confirm that how many rows you have to fetch
Yes , it should be predefined about how many rows he should fetch.
Check out my solution and if you find it dynamic then please upvote :)
I already mentioned , there are many ways to fetch . But in question he not mention dynamic , he knows how many rows he had to fetch.
Ok but the answer should always be dynamic so that if in future anyone needs solution it should be beneficial for everyone.
0

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

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.