jeudi 2 avril 2015

Trouble using INNER JOIN in SQLite in Android

I have a MySQL database at a local server. I fetch the data from the MySQL database using PHP and JSON over HTTP. In the Android app, an SQLite database is populated using the JSON result. I have verified that the SQLite database is actually populated by running test queries similar to



Cursor cursor = database.rawQuery("SELECT recipe_id, ingredient_id, name FROM mm_ingredient_in_recipe_groups", null);


and then iterating the resulting cursor to see that the data is present. The data that is present is stored in three tables:



  1. mm_ingredients, containing ingredients for recipes.

  2. mm_recipes, containing data for recipes.

  3. mm_ingredient_in_recipe, containing data for joining the data in mm_recipes and mm_ingredients (i.e. resolving the many-to-many relationship).


What I want to do is to run a query that selects ingredients for an argument recipe name. This means that I have to INNER JOIN mm_ingredients with mm_ingredient_in_recipe and also with mm_recipes.


I have tried running the following query:



database.rawQuery("SELECT ingredient.name, ingredient.base_unit FROM mm_ingredients AS ingredient INNER JOIN mm_ingredient_in_recipe AS ir ON ir.ingredient_id = ingredient._id INNER JOIN mm_recipes AS recipe ON ir.recipe_id = recipe._id WHERE recipe.name = ?", new String[] {"Spicy tomatsuppe"});


This returns an empty cursor. In my MySQL database, however, running the corresponding query



SELECT ingredient.name, ingredient.base_unit FROM mm_ingredients AS ingredient INNER JOIN mm_ingredient_in_recipe AS ir ON ir.ingredient_id = ingredient.id INNER JOIN mm_recipes AS recipe ON ir.recipe_id = recipe.id WHERE recipe.name = "Spicy tomatsuppe";


returns the expected data. What am I doing wrong here? It seems like every query I run works fine up to the point where I start including INNER JOINs. (The id fields are named _id in SQLite, as should be best practice as far as I know.)


Aucun commentaire:

Enregistrer un commentaire