我有两个类,库存和食谱。我遇到的问题是我无法将项目添加到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();
这很好用。项目以我希望的方式添加到Inventory。
但是,当我尝试将项目放入Recipe表时,使用以下两行(在主活动中初始化)。在主要活动中,我有一个方法,这是一个用于调试目的的虚拟方法。
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);
}
为什么这不起作用?对我来说,似乎这两个类及其用法彼此相同,只有一个有效。