在Android中的SQLite中使用INNER JOIN时出现问题

时间:2015-04-02 16:51:10

标签: android sqlite android-sqlite

我在本地服务器上有一个MySQL数据库。我使用PHP和JSON通过HTTP从MySQL数据库中获取数据。在Android应用程序中,使用JSON结果填充SQLite数据库。我已经验证了SQLite数据库实际上是通过运行类似于

的测试查询来填充的
Cursor cursor = database.rawQuery("SELECT recipe_id, ingredient_id, name FROM mm_ingredient_in_recipe_groups", null);

然后迭代生成的光标以查看数据是否存在。存在的数据存储在三个表中:

  1. mm_ingredients,含有食谱成分。
  2. mm_recipes,包含食谱数据。
  3. mm_ingredient_in_recipe,其中包含用于加入mm_recipesmm_ingredients中数据的数据(即解决多对多关系)。
  4. 我想要做的是运行一个查询,为参数配方名称选择成分。这意味着我必须与mm_ingredients以及mm_ingredient_in_recipe一起加入mm_recipes

    我尝试过运行以下查询:

    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"});
    

    返回一个空光标。但是,在我的MySQL数据库中,运行相应的查询

    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";
    

    返回预期的数据。我在这做错了什么?似乎我运行的每个查询都可以正常工作,直到我开始包含INNER JOIN。 (在SQLite中,id字段被命名为_id,据我所知应该是最佳实践。)

    编辑:添加用于写入数据库的代码:

    下面是我如何填充表mm_recipes的数据库的示例。它与其他两个表类似。我将JSONArray.optString()用于SQLite的TEXT数据类型,JSONArray.optInt()用于INTEGERJSONArray.optDouble()用于REAL

    public static final String TABLE_NAME                       = "mm_recipes";
    public static final String TABLE_NAME_IN_JSON               = "mm_recipes";
    
    public static final String COLUMN_ID                        = "_id";
    public static final String COLUMN_NAME                      = "name";
    public static final String COLUMN_IN_APP                    = "in_app";
    public static final String COLUMN_DIFFICULTY                = "difficulty";
    public static final String COLUMN_TIME_CONSUMPTION_UPPER    = "time_consumption_upper";
    public static final String COLUMN_TIME_CONSUMPTION_LOWER    = "time_consumption_lower";
    public static final String COLUMN_PREPARATION_TIME          = "preparation_time";
    public static final String COLUMN_PORTIONS                  = "portions";
    public static final String COLUMN_PORTIONS_UNIT             = "portions_unit";
    public static final String COLUMN_DESCRIPTION               = "description";
    public static final String COLUMN_TIPS                      = "tips";
    public static final String COLUMN_USE_INGREDIENT_GROUPS     = "use_ingredient_groups";
    
    public static final void populateDatabase(SQLiteDatabase database, JSONObject databaseData) throws JSONException{
    
        JSONArray table_data = databaseData.optJSONArray(TABLE_NAME_IN_JSON);
        ContentValues contentValues = new ContentValues();
    
        for (int i = 0; i < table_data.length(); i++){
            JSONObject data = table_data.getJSONObject(i);
    
            contentValues.clear();
    
            contentValues.put(COLUMN_NAME,                      data.optString(COLUMN_NAME));
            contentValues.put(COLUMN_IN_APP,                    data.optInt(COLUMN_IN_APP));
            contentValues.put(COLUMN_DIFFICULTY,                data.optString(COLUMN_DIFFICULTY));
            contentValues.put(COLUMN_TIME_CONSUMPTION_UPPER,    data.optInt(COLUMN_TIME_CONSUMPTION_UPPER));
            contentValues.put(COLUMN_TIME_CONSUMPTION_LOWER,    data.optInt(COLUMN_TIME_CONSUMPTION_LOWER));
            contentValues.put(COLUMN_PREPARATION_TIME,          data.optInt(COLUMN_PREPARATION_TIME));
            contentValues.put(COLUMN_PORTIONS,                  data.optInt(COLUMN_PORTIONS));
            contentValues.put(COLUMN_PORTIONS_UNIT,             data.optString(COLUMN_PORTIONS_UNIT));
            contentValues.put(COLUMN_DESCRIPTION,               data.optString(COLUMN_DESCRIPTION));
            contentValues.put(COLUMN_TIPS,                      data.optString(COLUMN_TIPS));
            contentValues.put(COLUMN_USE_INGREDIENT_GROUPS,     data.optInt(COLUMN_USE_INGREDIENT_GROUPS));
    
            database.insert(TABLE_NAME, null, contentValues);
        }
    
    }
    

    作为参考,这里也是一个如何创建mm_recipe表的示例。在我的班级mm_recipe

    public static final String DATATYPE_ID                      = "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL";
    public static final String DATATYPE_NAME                    = "TEXT NOT NULL";
    public static final String DATATYPE_IN_APP                  = "INTEGER DEFAULT 0";
    public static final String DATATYPE_DIFFICULTY              = "TEXT NOT NULL";
    public static final String DATATYPE_TIME_CONSUMPTION_UPPER  = "INTEGER NOT NULL";
    public static final String DATATYPE_TIME_CONSUMPTION_LOWER  = "INTEGER NOT NULL";
    public static final String DATATYPE_PREPARATION_TIME        = "INTEGER";
    public static final String DATATYPE_PORTIONS                = "INTEGER NOT NULL";
    public static final String DATATYPE_PORTIONS_UNIT           = "TEXT NOT NULL";
    public static final String DATATYPE_DESCRIPTION             = "TEXT";
    public static final String DATATYPE_TIPS                    = "TEXT";
    public static final String DATATYPE_USE_INGREDIENT_GROUPS   = "INT";
    
    /**Create table statement.*/
    public static final String CREATE_TABLE = 
        "CREATE TABLE"                  + " " + TABLE_NAME                      + " (" +
        COLUMN_ID                       + " " + DATATYPE_ID                     + ", " +
        COLUMN_NAME                     + " " + DATATYPE_NAME                   + ", " +
        COLUMN_IN_APP                   + " " + DATATYPE_IN_APP                 + ", " +
        COLUMN_DIFFICULTY               + " " + DATATYPE_DIFFICULTY             + ", " +
        COLUMN_TIME_CONSUMPTION_UPPER   + " " + DATATYPE_TIME_CONSUMPTION_UPPER + ", " +
        COLUMN_TIME_CONSUMPTION_LOWER   + " " + DATATYPE_TIME_CONSUMPTION_LOWER + ", " +
        COLUMN_PREPARATION_TIME         + " " + DATATYPE_PREPARATION_TIME       + ", " +
        COLUMN_PORTIONS                 + " " + DATATYPE_PORTIONS               + ", " +
        COLUMN_PORTIONS_UNIT            + " " + DATATYPE_PORTIONS_UNIT          + ", " +
        COLUMN_DESCRIPTION              + " " + DATATYPE_DESCRIPTION            + ", " +
        COLUMN_TIPS                     + " " + DATATYPE_TIPS                   + ", " +
        COLUMN_USE_INGREDIENT_GROUPS    + " " + DATATYPE_USE_INGREDIENT_GROUPS  + ");";
    

    在我的SQLiteOpenHelper课程中:

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(Table_mm_recipes.CREATE_TABLE);
        database.execSQL(Table_mm_ingredient_in_recipe.CREATE_TABLE);
        database.execSQL(Table_mm_ingredients.CREATE_TABLE);
    } 
    

1 个答案:

答案 0 :(得分:0)

事实证明这是(正如预期的)一些愚蠢的错误。我会把这个留在这里以防其他人犯同样的错误。

因为我每次都设置了代码来删除创建数据库中的所有内容,所以这样:

database.execSQL("DELETE FROM " + Table_mm_recipes.TABLE_NAME);
database.execSQL("DELETE FROM " + Table_mm_ingredient_in_recipe.TABLE_NAME);
database.execSQL("DELETE FROM " + Table_mm_ingredients.TABLE_NAME);
某种方式,数据发生了不好的事情。这样做没有多大意义,因为你可以创建数据库并在SQLiteOpenHelper.onCreate()插入数据并且没问题。如果您需要在设置任何版本控制之前更新数据,只需卸载并重新安装应用程序即可。然后,如果愿意,您可以在SQLiteOpenHelper.onUpgrade()销毁数据。我不确定如何数据搞砸了,但他们肯定是。

的奇怪之处在于,我仍然可以通过做一个简单的

看到数据库中有数据
Cursor cursor = database.rawQuery("SELECT recipe_id, ingredient_id, name FROM mm_ingredient_in_recipe_groups", null);

并从之前迭代该游标。我在INNER JOIN语句之前直接对所有表执行了此操作,但它们仍然无效。它可能与错误删除/创建数据库有关。

除此之外,上述所有代码都应该有效且有效。