Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a sqlite database in android , for example :

Table 1 person:

  • column 1: id
  • column 2: name
  • column 3: friends

Table 2 friends:

  • column 1: id
  • column 2: allFriends

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.

share|improve this question
1  
Seems very simple, so what have you tried? – Roope Jul 25 '16 at 8:51
up vote 1 down vote accepted

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;
  }
share|improve this answer
    
what is the person P and friends F ? – Daniel Levi Jul 25 '16 at 9:04
    
Its an Aliases. Read more from w3schools.com/sql/sql_alias.asp – Pramod Waghmare Jul 25 '16 at 9:05
    
SELECT * should be always avoided. Use field lists instead. – Rotwang Jul 25 '16 at 9:25

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.

share|improve this answer
    
Ofc. It is not copy-paste answer. You need to figure out a little bit by yourself to solve your question. – Stanislav Parkhomenko Jul 25 '16 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.