Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to iterate through a joined table with multiple results?
    primarykey
    data
    text
    <p>I have 3 tables, <code>USER</code>, <code>ENTRY</code> (for entered products, not necessary to create a PRODUCT table), and <code>USER_COLLECTION</code>, which is a table inbetween <code>USER</code> and <code>ENTRY</code>, because an entry can have multiple users.</p> <p>Basically:</p> <p>User = <code>USERID | USER_NAME</code></p> <p>Entry = <code>ENTRYID | ENTRY_NAME | ENTRYPRICE | ENTRY_DATE</code></p> <p>Collection = <code>COLLECTIONID | ENTRYID | USERID</code></p> <p>I have a table with users that persist throughout the project. They can create entries (which is usually some kind of product with a price) and they can link multiple users to a certain entry (which can be selected from a list, hence the users persist throughout the project).</p> <p>So for instance, my tables look like this:</p> <pre><code>User -------------------------- user_id | user_name -------------------------- 1 | 'FOO' 2 | 'BAR' 3 | 'FOOBAR' ENTRY ----------------------------------------------------------------------- entryid | entry_name | entry_price | entry_date ----------------------------------------------------------------------- 0 | 'Banana' | 2.50 | 12/12/2012 COLLECTION --------------------------------------- collectionid | entryid | userid ---------------------------------------- 0 | 1 | 1 1 | 1 | 2 2 | 1 | 3 </code></pre> <p>I have a Banana, with a price of 2.50 and 3 users linked to it, Foo, Bar and Foobar.</p> <p>Now, I want to use this in my app and get the data; except I don't know where to start. I tried selecting the entry data, using that id to loop through the collection data, but that would mean I have two cursors open and it wouldn't work. Tried creating a join but I couldn't really make a good one, mainly because:</p> <pre><code>JOIN --------------------------------------- collectionid | entryname | username ---------------------------------------- 0 | Banana | FOO 1 | Banana | BAR 2 | Banana | FOOBAR </code></pre> <p>I can't iterate through this, because I would create multiple of the same entry objects in my Android code...</p> <p>Hope I'm being clear on this.</p> <pre><code>if (cursor2.moveToFirst()) { do { Item i = new Item(&lt;GET STUFF FROM CURSOR&gt;); i.addUser(new Person(&lt;GET STUFF FROM CURSOR&gt;))); Log.d("TAG", i.getUsersPaying().size() + ""); } while (cursor2.moveToNext()); } </code></pre> <p>If I use this, I create mulitple instances of Item i. They'll all be Banana, whilst I should only have 1 item Banana, with multiple users added to it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload