So I’m trying to make a database which has name, calorie, and protein columns. Here is the create statement:
public class SQLHelp extends SQLiteOpenHelper{
static final String DATABASE_NAME = "Food.db";
static final String TABLE_FOOD = "food";
static final String NAME_COLUMN = "FoodName";
static final String CALORIE_COLUMN = "Calorie";
static final String PROTEIN_COLUMN = "Protein";
static final String FAT_COLUMN = "Fat";
static final String COLUMN_ID = "id";
public SQLHelp(Context c){
super(c, DATABASE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db){
String makeTable = "CREATE TABLE " + TABLE_FOOD + "("
+ NAME_COLUMN + " TEXT," + CALORIE_COLUMN + " INTEGER,"
+ PROTEIN_COLUMN + " INTEGER" + ")";
db.execSQL(makeTable);
}
The values are added to the database with the following method:
public void createFood(Food f) {
ContentValues values = new ContentValues();
values.put(SQLHelp.CALORIE_COLUMN, f.getCalories());
values.put(SQLHelp.PROTEIN_COLUMN, f.getProtein());
values.put(SQLHelp.NAME_COLUMN, f.getName());
database.insert(SQLHelp.TABLE_FOOD, null,
values);
}
When it tries to add, LogCat gives me an SQLiteException saying that the Protein column doesn’t exist. What’s even stranger is that when I run the code with commented out protein lines, everything works just fine.
Thanks!
Your DB helper only updates the database when you update the version number. You are running your version #1 DB which did not include those columns.
if you change your version to 2 it will reflect your newly added columns