SQLiteDatabase查询方法

时间:2018-01-05 15:17:38

标签: android database android-sqlite

我正在使用SQLiteDatabase的查询方法。如何通过特定STRING进行查询?

此查询:

    public ArrayList<RecipeInfo> getTypeRecipe() {
    ArrayList<RecipeInfo> recipeInfo = new ArrayList();
    Cursor cursor = null;
    try {
        cursor = db.query(TABLE_RECIPES_NAME, TABLE_RECIPE_COLUMNS,
                 RECIPE_COLUMN_TITLE+" =lunch", null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            RecipeInfo ri=cursorToRecipes(cursor);
            recipeInfo.add(ri);
            cursor.moveToNext();
        }

    }
    catch (Throwable t){
        t.printStackTrace();
    }
    return recipeInfo;
}

2 个答案:

答案 0 :(得分:0)

//database info
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "cookpadDB";

//recipe table
private static final String TABLE_RECIPES_NAME ="recipes";
private static final String RECIPE_COLUMN_ID ="id";
private static final String RECIPE_COLUMN_TITLE ="title";
private static final String RECIPE_COLUMN_DESCRIPTION ="description";
private static final String RECIPE_COLUMN_FOOD_IMAGE = "food_image";
private static final String RECIPE_COLUMN_TYPE="type";
private static final String RECIPE_COLUMN_LIKE="like";
private static final String[] TABLE_RECIPE_COLUMNS = {RECIPE_COLUMN_ID, RECIPE_COLUMN_TITLE,
        RECIPE_COLUMN_DESCRIPTION, RECIPE_COLUMN_FOOD_IMAGE,RECIPE_COLUMN_TYPE,RECIPE_COLUMN_LIKE};

答案 1 :(得分:0)

如果您希望将与特定字符串对应的记录作为其标题(例如,title =&#34; lunch&#34;),则执行以下查询:

cursor=db.query(TABLE_RECIPES_NAME, TABLE_RECIPE_COLUMNS,
                 "title=?", new String[]{"lunch"}, null, null, null);

OR

cursor=db.query(TABLE_RECIPES_NAME, TABLE_RECIPE_COLUMNS,
                     RECIPE_COLUMN_TITLE+"=?", new String[]{"lunch"}, null, null, null);

这将查询表TABLE_RECIPES_NAME并返回TABLE_RECIPE_COLUMNS指定的所有列,对应于&#34; title&#34;是&#34;午餐&#34;。