Daniel Levi
Daniel Levi

Reputation: 291

sqlite database android get all column values into string array

I have a sqlite database in android , for example :

Table 1 person:

Table 2 friends:

the id is the same in both tables, id in table1 == id in table2 i want to get all the values from table2 in specific column "allFriends" by the id and insert all the String values from the column into String array / arrayList.

Upvotes: 0

Views: 5168

Answers (2)

Pramod Waghmare
Pramod Waghmare

Reputation: 1283

Try this,

  public ArrayList<String> getAllFriends(int id) {
    ArrayList<String> friendsNames = new ArrayList<>();
    SQLiteDatabase sqLiteDatabase = null;
    try {
        String query = "select * from person P join friends F on F.id = P.id where P.id = " + id;
        Cursor cursor = sqLiteDatabase.rawQuery(query, null);
        while (cursor.moveToNext()) {
            friendsNames.add(cursor.getString(cursor.getColumnIndex("allFriends")));
        }
    }catch(Exception ex){
        Log.e(TAG,"Erro in geting friends "+ex.toString());
    }
    return friendsNames;
  }

Upvotes: 2

Stanislav Parkhomenko
Stanislav Parkhomenko

Reputation: 489

Your SQL query:

SELECT allFriends FROM friends where id == Id_from_person

Id_from_person - id of person who friends you want to recieve from DB.
For executing query read this.

Upvotes: 0

Related Questions