database - Android Studio, SQLite, No Such Column Issue -


this question has answer here:

below error i'm getting:

java.lang.runtimeexception: unable 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 recipes id=? 

this after i've uninstalled app , cleared data make sure don't have previous database messing me up. below code database class:

package groceryproject.jacob.com.recipelist;  /**  * created 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 {      // 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 array list of strings 1 string seperated tabs         string directionsconcat = "";         string ingedientsconcat = "";          if(recipe.getingredients() != null) {             stringbuilder ingred = new stringbuilder();             (string s : recipe.getingredients()) {                 ingred.append(s);                 ingred.append("\t");             }             ingedientsconcat = ingred.tostring();         }          if(recipe.getdirections() != null) {             stringbuilder direct = new stringbuilder();             (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 error points         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  * " + table_recipes;          sqlitedatabase db = this.getwritabledatabase();         cursor cursor = db.rawquery(selectquery, null);           if (cursor.movetofirst()) {             {                 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 array list of strings 1 string seperated tabs         string directionsconcat = "";         string ingedientsconcat = "";          if(recipe.getingredients() != null) {             stringbuilder ingred = new stringbuilder();             (string s : recipe.getingredients()) {                 ingred.append(s);                 ingred.append("\t");             }             ingedientsconcat = ingred.tostring();         }          if(recipe.getdirections() != null) {             stringbuilder direct = new stringbuilder();             (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  * " + table_recipes;         sqlitedatabase db = this.getreadabledatabase();         cursor cursor = db.rawquery(countquery, null);         cursor.close();          // return count         return cursor.getcount();     }    } 

the issue caused when call getrecipe() main activity class:

package groceryproject.jacob.com.recipelist;  import android.app.activity; import android.content.contentvalues; import android.content.context; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.os.parcelable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.view; import android.widget.button; import java.util.arraylist; import java.util.list;  public class recipelist extends appcompatactivity{     private recyclerview mrecyclerview;     private recyclerview.adapter madapter;     private recyclerview.layoutmanager mlayoutmanager;     private int request_code=1;      //private sqlitedatabase mdatabase;      //todo: create new taskbar     //todo: create navigaton bar.     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          recipedb dbhelper = new recipedb(this);          /*         if(savedinstancestate != null){             recipes = savedinstancestate.getparcelablearraylist("savedrecipes");         }         */            arraylist<string> 1 = new arraylist<>();         one.add("test");          recipe testrecipe = new recipe("name", "four slices",  "40", "80", one, one);          dbhelper.addrecipe(testrecipe);         recipe testtwo = dbhelper.getrecipe(1); //this breaks           list<recipe> recipes = dbhelper.getallrecipes();         recipes.add(testtwo); //this prove adapter working           string log = "no results";         (recipe rn : recipes){             log = "id: " + rn.getid() + ", name: " + rn.getrecipename();         }         log.d("name, ", log);         //this log printing no results when app runs           setcontentview(r.layout.activity_recipe_list);         mrecyclerview = (recyclerview) findviewbyid(r.id.list_recycler_view);           mlayoutmanager = new linearlayoutmanager(this);         mrecyclerview.setlayoutmanager(mlayoutmanager);           madapter = new myadapter(recipes);         mrecyclerview.setadapter(madapter);         } 

the cursor line in getrecipe() error log pointing to. happens when call getrecipe(). if create list calling getallrecipes() instead, , pass adapter, doesn't crash on start arraylist empty.

i know adapter works because if manually add array list recipes appears on screen correctly. it's database that's causing problem.

i've been working @ days, , have tried looking @ many similar issues on so, , feel stuck. advice appreciated.

check create_contacts_table variable, don't see preptime key in there. , contacts strange name recipe table variable....

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

after add column, need update database_version reflect changes.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -