我有两个类(class),库存和配方。我遇到的问题是我无法将项目添加到配方表中。我收到以下错误消息:
java.lang.IllegalStateException: Caused by: android.database.sqlite.SQLiteException: table RECIPE has no column named NAME (code 1): , while compiling: INSERT OR REPLACE INTO RECIPE(ID,INGREDIENTS,HOW_TO,NAME) VALUES (?,?,?,?)
当我启动位于 MainActivity
中的方法 goToSearchResults(View view)
时,会发生这种情况。
库存如下:
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 = new Inventory(foodType, foodName.getText().toString(), foodQuantity.getText().toString());
inventory.save();
这很好用。元素按照我想要的方式添加到库存中。
但是,当我尝试使用以下两行(在主 Activity 中初始化)将项目放入配方表中时。在主要 Activity 中,我有一个方法,它是用于调试目的的虚拟方法。
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);
}
为什么这不起作用?对我来说,这两个类及其用法似乎彼此相同,并且只有一个有效。
最佳答案
关于java - 使用 SugarORM 创建多个表会出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977903/