mardi 26 avril 2016

Many to Many Joins in SQLite on Android

I'm trying to grab some data from a database in my Android app. I have Containers and Widgets. Each Container can hold many Widgets and each Widget can be in many Containers. They are super simple:

containers
_id, title

widgets
_id, name

I have a containers_widgets table containing a column for container_id and a column for widget_id:

containers_widgets
containers_id, widget_id

I'm trying to grab a container with its relevant tags, so I have the following query:

SELECT containers.*, widgets.* FROM containers 
JOIN containers_widgets ON containers._id = containers_widgets.containers_id 
JOIN widgets ON widgets._id = containers_widgets.tag_id 
WHERE containers._id = 1

However, this throws the following error:

 android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

No SQL error is thrown, and both the Container and Widget tables are populated, as I can list them in other Fragments. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire