Android Studio,SQLite,没有这样的专栏问题

Android Studio,SQLite,没有这样的专栏问题,android,database,sqlite,android-sqlite,Android,Database,Sqlite,Android Sqlite,下面是我得到的错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{groceryproject.jacob.com.recipelist/groceryproject.jacob.com.recipelist.RecipeList}: android.database.sqlite.SQLiteException: no such column: prep_time (code 1): , while co

下面是我得到的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{groceryproject.jacob.com.recipelist/groceryproject.jacob.com.recipelist.RecipeList}: android.database.sqlite.SQLiteException: no such column: prep_time (code 1): , while compiling: SELECT id, recipe_name, servings, prep_time, cook_time, ingredients, directions FROM recipes WHERE id=?
这是在我卸载应用程序并清除所有数据以确保没有以前的数据库把我搞砸之后。下面是我的数据库类的代码:

package groceryproject.jacob.com.recipelist;

/**
 * Created by Jacob on 11/12/2016.
 */
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

public class RecipeDB extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "recipeManager";

    // Contacts table name
    private static final String TABLE_RECIPES = "recipes";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "recipe_name";
    private static final String KEY_COOK_TIME = "cook_time";
    private static final String KEY_PREP_TIME = "prep_time";
    private static final String KEY_SERVINGS = "servings";
    private static final String KEY_INGREDIENTS = "ingredients";
    private static final String KEY_DIRECTIONS = "directions";

    public RecipeDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RECIPES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_SERVINGS + " TEXT," + " TEXT," + KEY_COOK_TIME + " TEXT,"
                + KEY_INGREDIENTS + " TEXT," + KEY_DIRECTIONS + " TEXT" + ")";

        db.execSQL(CREATE_CONTACTS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECIPES);

        // Create tables again
        onCreate(db);
    }



    void addRecipe(Recipe recipe) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, recipe.getRecipeName()); // Contact Name
        values.put(KEY_SERVINGS, recipe.getServings()); // Contact Phone
        values.put(KEY_PREP_TIME, recipe.getPrepTime());
        values.put(KEY_COOK_TIME, recipe.getCookTime());

        //Next few lines turns an array list of strings into one string seperated by tabs
        String directionsConcat = "";
        String ingedientsConcat = "";

        if(recipe.getIngredients() != null) {
            StringBuilder ingred = new StringBuilder();
            for (String s : recipe.getIngredients()) {
                ingred.append(s);
                ingred.append("\t");
            }
            ingedientsConcat = ingred.toString();
        }

        if(recipe.getDirections() != null) {
            StringBuilder direct = new StringBuilder();
            for (String s : recipe.getDirections()) {
                direct.append(s);
                direct.append("\t");
            }
            directionsConcat = direct.toString();
        }

        values.put(KEY_INGREDIENTS, ingedientsConcat);
        values.put(KEY_DIRECTIONS, directionsConcat);


        db.insert(TABLE_RECIPES, null, values);
        db.close(); // Closing database connection
    }


    Recipe getRecipe(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_RECIPES, new String[] { KEY_ID,
                        KEY_NAME, KEY_SERVINGS, KEY_PREP_TIME, KEY_COOK_TIME, KEY_INGREDIENTS, KEY_DIRECTIONS },
                        KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null); //This line is where the error points to
        if (cursor != null)
            cursor.moveToFirst();

        List<String> directionsList = new ArrayList<>();
        List<String> ingredientsList = new ArrayList<>();

        String ingredients = cursor.getString(5);
        String directions = cursor.getString(6);


        if (directions != null){
            if(directions.contains("\t")) {
                directionsList = Arrays.asList(directions.split("\t"));
            }
            else{
                directionsList = Arrays.asList(directions);
            }
        }

        if (ingredients != null){
            if(ingredients.contains("\t")) {
                ingredientsList = Arrays.asList(ingredients.split("\t"));
            }
            else{
                ingredientsList = Arrays.asList(ingredients);
            }
        }

        Recipe recipe = new Recipe(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4), ingredientsList, directionsList);
        return recipe;
    }

    public List<Recipe> getAllRecipes() {
        List<Recipe> recipeList = new ArrayList<Recipe>();

        String selectQuery = "SELECT  * FROM " + TABLE_RECIPES;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);


        if (cursor.moveToFirst()) {
            do {
                Recipe recipe = new Recipe();

                recipe.setID(Integer.parseInt(cursor.getString(0)));
                recipe.setRecipeName(cursor.getString(1));
                recipe.setServingSize(cursor.getString(2));
                recipe.setPrepTime(cursor.getString(3));
                recipe.setCookTime(cursor.getString(4));

                List<String> directionsList = new ArrayList<>();
                List<String> ingredientsList = new ArrayList<>();

                String ingredients = cursor.getString(5);
                String directions = cursor.getString(6);


                if (directions != null){
                    if(directions.contains("\t")) {
                        directionsList = Arrays.asList(directions.split("\t"));
                    }
                    else{
                        directionsList = Arrays.asList(directions);
                    }
                }


                if (ingredients != null){
                    if(ingredients.contains("\t")) {
                        ingredientsList = Arrays.asList(ingredients.split("\t"));
                    }
                    else{
                        ingredientsList = Arrays.asList(ingredients);
                    }
                }

                recipe.setIngredients(ingredientsList);
                recipe.setDirections(directionsList);

                recipeList.add(recipe);
            } while (cursor.moveToNext());
        }


        return recipeList;
    }


    public int updateRecipe(Recipe recipe) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, recipe.getRecipeName()); // Contact Name
        values.put(KEY_SERVINGS, recipe.getServings()); // Contact Phone
        values.put(KEY_PREP_TIME, recipe.getPrepTime());
        values.put(KEY_COOK_TIME, recipe.getCookTime());

        //Next few lines turns an array list of strings into one string seperated by tabs
        String directionsConcat = "";
        String ingedientsConcat = "";

        if(recipe.getIngredients() != null) {
            StringBuilder ingred = new StringBuilder();
            for (String s : recipe.getIngredients()) {
                ingred.append(s);
                ingred.append("\t");
            }
            ingedientsConcat = ingred.toString();
        }

        if(recipe.getDirections() != null) {
            StringBuilder direct = new StringBuilder();
            for (String s : recipe.getDirections()) {
                direct.append(s);
                direct.append("\t");
            }
            directionsConcat = direct.toString();
        }

        values.put(KEY_INGREDIENTS, ingedientsConcat);
        values.put(KEY_DIRECTIONS, directionsConcat);

        // updating row
        return db.update(TABLE_RECIPES, values, KEY_ID + " = ?",
                new String[] { String.valueOf(recipe.getID()) });
    }


    public void deleteRecipe(Recipe recipe) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_RECIPES, KEY_ID + " = ?",
                new String[] { String.valueOf(recipe.getID()) });
        db.close();
    }



    public int getRecipeCount() {
        String countQuery = "SELECT  * FROM " + TABLE_RECIPES;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }



}
getRecipe()
中的光标行是错误日志指向的位置。只有在调用
getRecipe()
时才会发生这种情况。如果我要通过调用
getAllRecipes()
创建一个列表,并将其传递给适配器,它在启动时不会崩溃,但ArrayList也是空的

我知道适配器可以工作,因为如果我手动将其添加到数组列表中,它会正确地显示在屏幕上。是数据库导致了这个问题


我已经在这方面工作了好几天了,并且试着在这方面寻找许多类似的问题,我只是觉得卡住了。非常感谢您的建议。

请检查
创建联系人表
变量,我在其中没有看到preptime键。Contacts是配方表变量的奇怪名称

    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RECIPES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_SERVINGS + " TEXT," + " TEXT," + KEY_COOK_TIME + " TEXT,"
            + KEY_INGREDIENTS + " TEXT," + KEY_DIRECTIONS + " TEXT" + ")";

添加列后,还需要更新
数据库版本
,以反映这些更改

您没有在oncreate命令中添加
KEY\u PREP\u TIME

谢谢,我觉得有点傻。是的,联系人是个奇怪的名字。因为这是我第一次使用SQL,所以我一直在学习一个教程。我第一次完全按照指南自己创建了这个类,但无法让它工作。因此,在几天的失败后,出于绝望,我复制并翻译了(错过了这一篇)。我只是没意识到我离答案这么近…再次感谢你!有很多库可以使SQLite变得更容易。例如,RealmDB是一种流行的。糖虫也是
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_RECIPES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_SERVINGS + " TEXT," + " TEXT," + KEY_COOK_TIME + " TEXT,"
            + KEY_INGREDIENTS + " TEXT," + KEY_DIRECTIONS + " TEXT" + ")";