mardi 8 décembre 2015

Android Raw Query

I am trying to implement a SQL query from within my android application using rawQuery. I know it isnt the prettiest looking bit of code but I know the SQL works and thats why I am using it. I am trying to produce the names of potential recipes I can cook based on the contents of contents table

However it isnt working in my application and I cant seem to locate where its gone wrong as I amnt getting an error just a blank page.

rawQuery

public Cursor possibleRecipes()
{
    return db.rawQuery("select\ting._id, rec._id, ing.recipe_code, \n" +
            "\trec.recipe_name, \n" +
            "\tcount(*) avail_ing, \n" +
            "\t\t(select count(*) from ingredients ing2\n" +
            "\t \twhere\ting2.recipe_code = ing.recipe_code\n" +
            "\t \tgroup by recipe_code) as ing_count,\n" +
            "\tcount(*)/(select count(*) from ingredients ing2\n" +
            "\t \twhere\ting2.recipe_code = ing.recipe_code\n" +
            "\t \tgroup by recipe_code)\n" +
            "from\tingredients ing,\n" +
            "\trecipes rec\n" +
            "where\tingredient_name in (select ingredient_name from kitchen)\n" +
            "and\ting.recipe_code = rec.recipe_code\n" +
            "group by ing.recipe_code, rec.recipe_name\n" +
            "having (count(*)/(select count(*) from ingredients ing2\n" +
            "\t \twhere\ting2.recipe_code = ing.recipe_code\n" +
            "\t \tgroup by recipe_code)) = 1\n" +
            "\n" +
            "\n" +
            "\n",null);
    //return db.query(INGREDIENTS_TABLE, new String[]{KEY_ID, KEY_INGREDIENT_NAME}, null, null, null, null, null);
}

Tables

//Recipe create statement
private static final String CREATE_TABLE_RECIPES = "CREATE TABLE "
        + RECIPE_TABLE + "("
        + KEY_ID + " INTEGER AUTO INCREMENT,"
        + KEY_CODE + " INTEGER AUTO INCREMENT PRIMARY KEY,"
        + KEY_RECIPE_NAME + " TEXT" + ")";

//Ingredient create statement
private static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE "
        + INGREDIENTS_TABLE + "("
        + KEY_ID + " INTEGER AUTO INCREMENT PRIMARY KEY,"
        + KEY_CODE + " INTEGER AUTO INCREMENT,"
        + KEY_INGREDIENT_NAME + " TEXT,"
        + " FOREIGN KEY ("+KEY_CODE+") REFERENCES "+RECIPE_TABLE+"("+KEY_CODE+"));";


//Contents create statement
private static final String CREATE_TABLE_CONTENTS = "CREATE TABLE "
        + CONTENTS_TABLE + "("
        + KEY_ID + " INTEGER PRIMARY KEY,"
        + KEY_CONTENTS_NAME + " TEXT" + ")";

Class Calling query

DBMain adapter = new DBMain(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_possible_recipes);

    adapter.open();
    adapter.getAllRecipes();

    Cursor cursor = adapter.possibleRecipes();


    //String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
    String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};

    int[] to = new int[] {R.id.recipeName};

    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row1, cursor, columns, to, 0);

    ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
    recipeList.setAdapter(myCursorAdapter);

}

XML

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/title"
    android:text="Recipes!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/possibleRecipeList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/title"
    android:dividerHeight="0.1dp"
    >
</ListView>

</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire