Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine data from two separate tables into a single Cursor?

Currently, I have a table of posts and a table of users. Naturally, each user can be associated with multiple posts. Each row in the post table stores the user ID of the user that created the post. Here are example rows:

Post row: post_id headline user_id

User row: user_id user_name

I want to return a Cursor containing both the post row and its corresponding user row by matching the user ID in the post table to the user ID in the user table. What type of query would I use to achieve this? The result should be:

Combined row: post_id headline user_id user_name

More generally: How do I combine data from two separate tables based on a shared piece of data into a single Cursor?

like image 610
cheese1756 Avatar asked Jul 24 '13 19:07

cheese1756


1 Answers

You can use a CursorJoiner to get something similar to merging two Cursors into one. The CursorJoiner doesn't actually perform a merge. As you iterate over it, it moves the original two Cursors such that their rows will match up on the specified column(s). This is why it's necessary that both Cursors be sorted on the columns that are to be used in the join.

Link to documentation: http://developer.android.com/reference/android/database/CursorJoiner.html

Code example:

CursorJoiner joiner = new CursorJoiner(userCursor, new String[]{ "user_id" }, postCursor, new String[] {"user_id"});

while (joiner.hasNext()) {
    CursorJoiner.Result result = joiner.next();
        switch (result) {
            case LEFT:
                // don't care about this case
                break;

            case RIGHT:
                // nor this case
                break;

            case BOTH:
                // here both original Cursors are pointing at rows that have the same user_id, so we can extract values
                int postId = postCursor.getInt(...);
                String headline = postCursor.getString(...);
                int userId = userCursor.getInt(...);        
                String userName = userCursor.getString(...);

                // do something with above values

                break;

        }
}     
like image 99
ErikR Avatar answered Oct 19 '22 21:10

ErikR