所以我正在尝试创建一个包含名称、卡路里和蛋白质列的数据库。这是创建语句:
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);
}
使用以下方法将值添加到数据库中:
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);
}
当它尝试添加时,LogCat 给我一个 SQLiteException 说蛋白质列不存在。更奇怪的是,当我运行带有注释掉的蛋白质行的代码时,一切正常。
谢谢!