2

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!

1
  • Please post the code for your Food class, espeically the getters that you are using here. Also post the stack trace from logcat, at least until the first line that shows a method name from something you wrote. Commented Oct 5, 2012 at 21:52

1 Answer 1

3

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

 public SQLHelp(Context c){
        super(c, DATABASE_NAME, null, 2); //note the 2 here
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.