0

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.

1
  • 1
    Seems very simple, so what have you tried? Commented Jul 25, 2016 at 8:51

2 Answers 2

2

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;
  }
Sign up to request clarification or add additional context in comments.

3 Comments

what is the person P and friends F ?
Its an Aliases. Read more from w3schools.com/sql/sql_alias.asp
SELECT * should be always avoided. Use field lists instead.
0

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.

1 Comment

Ofc. It is not copy-paste answer. You need to figure out a little bit by yourself to solve your question.

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.