我有两个课程,Inventory和Recipe。我遇到的问题是我无法将项目添加到配方表中。我收到以下错误消息:
java.lang.IllegalStateException:引起:android.database.sqlite.SQLiteException:表 RECIPE 没有名为 NAME(代码 1)的列,编译时:INSERT OR REPLACE INTO RECIPE(ID,INGREDIENTS,HOW_TO,NAME) VALUES (? ,?,?,?)
当我启动该方法时会发生这种情况,该方法goToSearchResults(View view)
位于MainActivity
.
库存如下所示:
import com.orm.SugarRecord;
public class Inventory extends SugarRecord {
String foodType;
String foodName;
String foodQuantity;
public Inventory(){
}
public Inventory(String foodType, String foodName, String foodQuantity) {
this.foodType = foodType;
this.foodName = foodName;
this.foodQuantity = foodQuantity;
}
public String getFoodType() {
return foodType;
}
@Override
public String toString() {
return foodType +
" " + foodName + '\'' +
" " +foodQuantity + '\''
;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public String getFoodQuantity() {
return foodQuantity;
}
public void setFoodQuantity(String foodQuantity) {
this.foodQuantity = foodQuantity;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this. foodName = foodName;
}
}
类 Recipe 如下所示。
import com.orm.SugarRecord;
public class Recipe extends SugarRecord {
String name;
String ingredients;
String howTo;
public Recipe() {
}
public Recipe(String name, String ingredients, String howTo) {
this.name = name;
this.ingredients = ingredients;
this.howTo = howTo;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getHowTo() {
return howTo;
}
public void setHowTo(String howTo) {
this.howTo = howTo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Recipe{" +
"name='" + name + '\'' +
", ingredients='" + ingredients + '\'' +
", howTo='" + howTo + '\'' +
'}';
}
}
当我尝试将项目添加到 Inventory 表时,我使用以下两行:
inventory = new Inventory(foodType, foodName.getText().toString(), foodQuantity.getText().toString());
inventory.save();
这工作正常。项目以我希望的方式添加到库存中。
但是,当我尝试将项目放入配方表时,使用以下两行(在主活动中初始化)。在主要活动中,我有一个方法,这是一个用于调试目的的虚拟方法。
public void goToSearchResults(View view){
Intent intent = new Intent(this, SearchResults.class);
recipe = new Recipe("Filet Mignon", "Beef, Potatoes", "Use the owen to cook the potatoes real good -_^");
recipe.save();
startActivity(intent);
}
为什么这不起作用?对我来说,这两个类及其用法似乎彼此相同,只有一个有效。